c# - Building a custom predicate to act as a filter using a foreach loop -


i need filter list of documents passing them custom filter i'm struggling build dynamically using foreach loop :

var mainpredicate = predicatebuilder.true<document>();  // mainpredicate combined other filters here ...  var innerpredicate = predicatebuilder.false<document>(); foreach (var period in periods) {     var p = period;     expression<func<document, bool>> inperiod =         d => d.date >= p.datefrom && d.date <= p.dateto;      innerpredicate = innerpredicate.or(d => inperiod.invoke(d)); }  mainpredicate = mainpredicate.and(innerpredicate); 

this last line :

documents = this.objectset.asexpandable().where(mainpredicate).tolist(); 

throws exception :

the parameter 'd' not bound in specified linq entities query expression.

anyone knows why i'm getting exception ? don't understand 'd' parameter passing inperiod method gets lost. don't know missing work. code same many other examples work perfectly. additionnal theoric theoric information invoking expressions , how works behind scenes welcome.

i don't understand why this:

innerpredicate = innerpredicate.or(d => inperiod.invoke(d)); 

when avoid invoke completely, this:

innerpredicate = innerpredicate.or(inperiod); 

this should work fine.


btw, have feeling there's bug linqkit here (unless there's documentation suggests doesn't support scenario).

when tried similar code:

 expression<func<int, bool>> first = p1 => p1 > 4;  expression<func<int, bool>> second = p2 => p2 < 2;  // expand similar asexpandable, except works on  // expressions, not queryables. var composite = first.or(d => second.invoke(d))                      .expand(); 

...linqkit generated following composite expression:

p1 => ((p1 > 4) orelse (d < 2)) // on earth d? 

... indeed has unbound parameter d (nodetype = parameter, name = 'd').

dodging invoke first.or(second).expand() generates sensible:

p1 => ((p1 > 4) orelse (p1 < 2)) // better now... 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -