How to use Dynamic LINQ (System.Linq.Dynamic) for LIKE operation?

前端 未结 5 1759
萌比男神i
萌比男神i 2021-02-04 07:31

Can any body tell me how can I use a LIKE operator using System.Linq.Dynamic?

I need to add more than one LIKE expression in my dynamic where query

5条回答
  •  灰色年华
    2021-02-04 08:12

    Just add more where clauses

    var query = db.Customers.Where(c=>c.CityName.contains("London"));
    query = query.Where(c=>c.CityName.contains("USA"));
    query = query.Where(c=>c.CityName.contains("Johannesburg"));
    

    but the above query will create it :

    select * from Customer where CityName like "london" and CityName like "USA" etc...

    you want

    select * from Customer where CityName like "london" or CityName like "USA" etc...

    To use Dynamic Created or statements you can use predicatebuilder there's really alot of functionality there that you can use...

    http://www.albahari.com/nutshell/predicatebuilder.aspx

    var predicate = PredicateBuilder.False();
    predicate = predicate.Or(c=>c.CityName.Contains("London"));
    predicate = predicate.Or(c=>c.CityName.Contains("USA"));
    predicate = predicate.Or(c=>c.CityName.Contains("Johannesburg"));
    

提交回复
热议问题