简体   繁体   中英

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.);
}

If this is EF Core than the following can be used.

public static  List<Categories> CategoriesLike(string keyword)
{
    using var dbContext = new NorthwindContext();

    return dbContext.Categories.Where(category => 
        EF.Functions.Like(category.CategoryName, $"{keyword}%"))
        .ToList();
}

Do like this.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM