The question is this. I want to create an API request to search for text in the database. When using the SQL query SELECT * FROM dbo.SlideDB WHERE TextSlide like '%TextTest%' in the Database I find all matches. On the Api side, it’s impossible to get all matches with the Like () function.

  [HttpGet] [Route("/search/{searchform}")] public ActionResult Search(string searchform) { // Находит не все соответствия var search = _context.SlideDB.Where(p => p.TextSlide == searchform); // Вылетает ошибка var search = _context.SlideDB.Where(p => EF.Functions.Like(p.TextSlide, "%{searchform}%")); return Ok(search); } 
  • What do you mean by "on the API side it is not possible to send a request"? Input data incorrectly fall on the action? Or does the query return not what is needed from the database? - AK
  • one
    _context.SlideDB.Where(p => p.TextSlide.Contains(searchform)); ? - tym32167
  • @AK rephrased - richardgir
  • @ tym32167 If there is no wildcard, then yes, it can be done simply via string.Contrains - but it doesn’t matter , the question is probably not what the database is looking for. - AK
  • one
    @AK I suggested another code not because the vehicle doesn’t know something, but because my version is a bit more compact and it is a bit more difficult to make a mistake in it. - tym32167

2 answers 2

You can either correct your code.

 var search = _context.SlideDB.Where(p => EF.Functions.Like(p.TextSlide, $"%{searchform}%")); 

either use a shorter recording of the same

 var search = _context.SlideDB.Where(p => p.TextSlide.Contains(searchform)); 

    The example is caused by typing or ignorance of line interpolation . It was necessary like this:

     EF.Functions.Like(p.TextSlide, $"%{searchform}%" 

    It was also possible to make a knight's move - use the usual string.Contains ( you can read about the difference here ):

     _context.SlideDB.Where(p => p.TextSlide.Contains(searchform));