简体   繁体   中英

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

I don't find a way to make a search on IQueryable object with LIKE %some%text% operator like in SQL command.

At the moment I do this:

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

But it don't find an entry without space in shop.Name . Anyone know a way to do this?

Thank you very much!

You can use SqlMethod in the namespace System.Data.Linq.SqlClient :

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

SqlMethods.Like Method (String, String)

In EF Context you can use SqlFunctions in the namespace System.Data.Objects.SqlClient.SqlFunctions

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

SqlFunctions.PatIndex Method (String, String)

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