lambda - linq query with dynamic predicates in where clause joined by OR -


you can create dynamic queries in c# if add more restrictions current query.

var list = new list<item>(); var q = list.asqueryable(); q = q.where(x => x.size == 3); q = q.where(x => x.color == "blue"); 

in case, every new predicate added performing , operation previous. previous result equivalent to:

q = list.where(x => x.size == 3 && x.color == "blue"); 

is possible achieve same result or instead of and?

q = list.where(x => x.size == 3 || x.color == "blue"); 

the idea have variable number of expressions joined or operator.

expected result need written in how similar following pseudo code:

var conditions = new list<func<item, bool>>(); 

and later iterate conditions build like:

foreach(var condition in conditions)     finalexpression += finalexpression || condition; 

thanks raphaƫl althaus gave following link:

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

predicate builder solution. can use installing linqkit nuget. in url can find implementation of class.

note: in order make work linqtosql or linqtoentities iqueriable object must transformed using "asexpandable()" method, memory objects it's not required


Comments

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -