C # Dynamic variable Linq Where clause

I am following Scott Gu's article to create a dynamic LINQ http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library. aspx

He gave an example:

Expression<Func<Customer, bool>> e1 = DynamicExpression.ParseLambda<Customer, bool>("City = \"London\""); Expression<Func<Customer, bool>> e2 = DynamicExpression.ParseLambda<Customer, bool>("Orders.Count >= 10"); IQueryable<Customer> query = db.Customers.Where("@0(it) and @1(it)", e1, e2); 

This works great in my case. However, I have an unknown number of where clauses, which is determined at runtime.

Can someone tell me how to create a universal Where clause, for example

 Where("@0(it) and @1(it) and... @n(it)", e1, e2, ... en); 

thanks

+7
source share
2 answers

You can attach additional operators to the query object:

  query = db.Customers.Where( ... ); query = query.Where( ... ); query = query.Where( ... ); 

Thus, you can attach offers dynamically, and you are not dependent on their number.

+18
source

Try it;

Dynamic linq query with multiple / unknown criteria

I had a similar problem and this was the solution I used (and still in use)

+1
source

All Articles