c# - Creating an (Lambda) Expression using recursion -


i have following dictionary:

dictionary<string, propertyinfo> filterproperties; 

the content of dictionary can this:

- "language": [querydata.lang], - "id": [querydata.userid] 

each string key maps property of querydata type.

now let's suppose have following querydata instance:

querydata: { lang= "en", userid = "mcicero" } 

using previous dictionary example, want build following expression:

e => e.language == "en" && e.id == "mcicero"; 

as can see dictionary keys used accesing properties of e, , dictionary values (querydata properties) used specifying constants in binary equal expressions.

e of type entity, , guaranteed have properties.

the resulting expression should of type expression<func<entity, bool>>

how can build expression using recursion?

i recursion because sounds natural solution, iterative 1 preferred.

i tried iterative alternative , ended ugly , not understandable code.

however, having trouble creating recursion method problem.

leaving creation of individual expression you, combine them in iterative loop:

// ienumerable<expression> in "expressions" expression left = null; foreach(var right in expressions) {   if(left == null)     left = right;   else     left = expression.and(left, right); } // combined expression in "left" // don't forget null if there no expressions provided... 

or in 1 line linq:

var expr = expressions.aggregate(expression.and); 

Comments