Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 505
alexandre-rousseau Avatar asked Apr 08 '16 14:04

alexandre-rousseau


1 Answers

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)

like image 187
Quentin Roger Avatar answered Sep 22 '22 21:09

Quentin Roger