c# - Apply Where conditionally (OR between them) LINQ -


i have filters in asp.net project , want add expression list condition:

expression<func<mymodel, bool>> func; var list = new list<expression<func<mymodel, bool>>>(); 

i want conditionally apply (or between them). example:

if(sth){    func = p => p.a <= b;    list.add(func); } if (sth else){    func = p => p.c >= d;    list.add(func); }  var fq = session.query<mymodel>(); fq = list.aggregate(fq, (current, expression) => current.where(expression)); 

how can this?

looks extension method

public static class enumerableextensions {     public static ienumerable<t> conditionalwhere<t>(this ienumerable<t> list,                                                 bool condition, func<t,bool> predicate)     {         if(!condition)              return list;         return list.where(predicate);     } } 

usage be:

var fq = session.query<mymodel>(); var result = fq.conditionalwhere(sth, p => p.a <= b)                .conditionalwhere(sth_else, p => p.c >= d); 

Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -