How can I utilize System.Linq.Dynamic for the LIKE operation?

asp.net c# dynamic-linq linq

Question

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

1
13
8/22/2019 1:49:26 AM

Accepted Answer

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

26
1/5/2011 2:26:46 AM

Popular Answer

This will allow the LIKE operator on integer fields:

.Where(searchField + ".ToString().Contains(@0)", searchString);


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow