Building a custom predicate to act as a filter using a foreach loop

c# expression-trees linq-expressions linqkit linq-to-entities


I need to filter a list of documents by passing them to a custom filter that I'm struggling to build dynamically using a foreach loop :

var mainPredicate = PredicateBuilder.True<Document>();

// mainPredicate is combined to other filters successfully 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 this exception :

The parameter 'd' was not bound in the specified LINQ to Entities query expression.

Anyone knows why I'm getting this exception ? I don't understand where the 'd' parameter I am passing to the InPeriod method gets lost. I don't know what is missing for this to work. My code is the same as many other examples that work perfectly. Any additionnal theoric theoric information about invoking expressions and how it works behind the scenes are welcome.

5/10/2013 2:47:44 PM

Accepted Answer

Finally, I have found a way to avoid combining multiple predicates to the main expression tree.

Given that each predicate represents a different filter and I want the final, combined filter to be a series of must-be-respected conditions, we can say that each of the predicates has to return true for the final predicate to return true.

For that to work, the predicates has to be combined with AND. So, the resulting SQL query must look like this :

predicate1 AND predicate2 AND predicate3 ...

A better way to combine these predicates with AND is to chain Where query operators to the final query, like this :

var documents = this.ObjectSet.AsExpandable()

The resulting SQL query will combine each of these predicates with AND. That is just what I wanted to do.

It is easier than hacking out an expression tree by myself.

5/13/2013 12:53:38 PM

Popular Answer

I don't understand why you do this:

innerPredicate = innerPredicate.Or(d => inPeriod.Invoke(d));

When you could just avoid the Invoke completely, like this:

innerPredicate = innerPredicate.Or(inPeriod);

This should work perfectly fine.

BTW, I have a feeling there's a bug with LINQKit here (unless there's some documentation that suggests that it doesn't support this scenario).

When I tried this similar code:

 Expression<Func<int, bool>> first = p1 => p1 > 4;
 Expression<Func<int, bool>> second = p2 => p2 < 2;

// Expand is similar to AsExpandable, except it works on 
// expressions, not queryables.
var composite = first.Or(d => second.Invoke(d))

...LINQKit generated the following composite expression:

p1 => ((p1 > 4) OrElse (d < 2)) // what on earth is d?

... which indeed has the unbound parameter d (NodeType = Parameter, Name = 'd').

Dodging the Invoke with first.Or(second).Expand() generates the perfectly sensible:

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

Related Questions

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow