C# - 如何使用Where Like%搜索IQueryable对象

时间:2016-04-08 14:23:51

标签: c# sql asp.net iqueryable

我没有找到一种方法在IQueryable对象上使用LIKE %some%text%运算符进行搜索,就像在SQL命令中一样。

目前我这样做:

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中找不到没有空格的条目。有人知道这样做的方法吗?

非常感谢!

1 个答案:

答案 0 :(得分:9)

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

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

SqlMethods.Like Method (String, String)

在EF上下文中,您可以在命名空间SqlFunctions

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

SqlFunctions.PatIndex Method (String, String)