C# - 如何在 IQueryable 对象中搜索 Where Like %

C# - How can I search in a IQueryable object with Where Like %

我找不到像 SQL 命令中那样使用 LIKE %some%text% 运算符搜索 IQueryable 对象的方法。

目前我这样做:

System.Data.Entity.DbSet<Shop> database_Shops = database.Shops;
string searched_shop = !String.IsNullOrEmpty(post_data["shop"]) ? post_data["shop"] : " ";

ViewBag.shops = database_Shops
                .Where(shop => shop.Name.ToUpper().Contains( searched_shop.ToUpper()))
                .Take(RESULTS_PER_PAGES * pageNumber);

但是它在 shop.Name 中找不到没有 space 的条目。有人知道这样做的方法吗?

非常感谢!

您可以在命名空间 System.Data.Linq.SqlClient 中使用 SqlMethod :

database_Shops.Where(shop => SqlMethods.Like(shop.Name, "%some%text%"));

SqlMethods.Like Method (String, String)

在 EF 上下文中,您可以在命名空间 System.Data.Objects.SqlClient.SqlFunctions

中使用 SqlFunctions
database_Shops.Where(shop => SqlFunctions.PatIndex("%some%text%", shop.Name) > 0);

SqlFunctions.PatIndex Method (String, String)