Search code examples
c#entity-frameworklinqsql-like

How to use LIKE Operator in LINQ to Entity Framework


I'm using Entity Framework.

Problem: search a name that start with 'a' in a list of strings:

var likeQuery = from k in dbContext.Categories  
                where sqlMethod.Like(k.CategoryName, "a%" )  // name start with a 
                select k;
        
foreach (var item in likeQuery)
{
    Console.WriteLine(Item.);
}

Solution

  • Do like this.

    var result 
      = dbContext.Categories.Where(i => i.CategoryName.StartsWith("a")).ToList();