c# - How to use the parenthesis in the expression tree -


[question] tell me how create appropriate expression tree?

[detail] created following query statically, , result expected.

// datasource     string[] datasource = { "john", "ken", "mark", "mary", "tom" };  // keyword search     string[] keywords = { "o", "mark", "tom" };       //linq query (static)     var query = datasource.asqueryable().     where(item =>    (item.tolower().contains(keywords[0].tolower()) ||    item.tolower().contains(keywords[1].tolower())) &&    item.tolower().contains(keywords[2].tolower())).     orderbydescending(item => item);   //result // "tom" 

condition || condition b && condition c

but not know how code following condition expression tree

(condition || condition b) && condition c

does tell me how use parethesis in expression tree? far created body lambda expression follows not work well.

public static expression getcontainsexpression(expression parameter, string keyword, expression curbody) {      var keywordvalue = expression.constant(keyword, typeof(string));       var newbody =       expression.equal( expression.call(parameter, tolower), expression.call(keywordvalue, tolower) );      ///connect curbody expression , newbody expression "or" e.s. ||        if (curbody != null)     {         if (keyword == "tom")         {             return expression.andalso(curbody, newbody);         }          else         return expression.orelse(curbody, newbody);     }     return newbody; } 

the parethesis created automaticaly. can't avoid it. expression.orelse or expression.andalso takes other expression left , right , if combined expressions binaryexpression wrapped in parethesis automatically.

have @ code:

var paramx = expression.parameter(typeof(bool), "x"); var paramy = expression.parameter(typeof(bool), "y"); var paramz = expression.parameter(typeof(bool), "z"); var expr = expression.andalso(expression.orelse(paramx, paramy), paramz); 

if call expr.tostring() you'll "((x orelse y) andalso z)". outer andalso-expression wrapped parethesis. there no way remove them (as know far).

a small hint: can call tostring() on every expression , return created code. knowing makes easier create dynamic expressions because you've small ability see get.


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -