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

Kiarash picture Kiarash · Jan 5, 2011 · Viewed 16.3k times · Source

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

/*
var query =
db.Customers.
Where("CityName Like @0 or CityName Like @1", "London", "USA")
*/
var query =
db.Customers.
Where("CityName Like @0 or CityName Like @1%", "London", "USA")

thanks heaps

Answer

Jaime picture Jaime · Jan 5, 2011

Try using simply "CityName.Contains(@1)" this will convert to the proper lambda since its a method invocation on an accessible type.

something like:

var query =
db.Customers.
Where("CityName.Contains(@0) or CityName.Contains(@1)", "London", "USA")

Just tested it with the sample app that comes with the dynamic library and it generates the LIKE operator