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
Post a Comment