Nesting PredicateBuilder predicates : 'The parameter 'f' was not bound in the specified LINQ to Entities query expression'

StackOverflow https://stackoverflow.com/questions/16462692

  •  21-04-2022
  •  | 
  •  

Question

I am building predicates using LinqKit's PrediateBuilder class to dynamically setup filters and I want to combine a nested one to another.

I have read this (http://www.albahari.com/nutshell/predicatebuilder.aspx) :

enter image description here

Here is my code :

// The main predicate.
var mainPredicate = PredicateBuilder.True<Document>();

// ... some other conditions to the main predicate here ...

// The inner predicate (combined conditions using OR).
var innerPredicate = PredicateBuilder.False<Document>();

foreach (var period in periods)
{
    var p = period;
    innerPredicate =
        innerPredicate.Or(
            d =>
            (d.Date >= p.DateFrom && d.Date <= p.DateTo));
}

mainPredicate = mainPredicate.And(innerPredicate);

documents = this.ObjectSet.AsExpandable().Where(mainPredicate).ToList();

I am combining my two predicates just like it is explained in the documentation. However, I get this exception :

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

I first thought that the inner predicate has to be expanded before combining it with the main predicate, so I changed my combining code to add a call to to the inner predicate's Expand method like this :

mainPredicate = mainPredicate.And(innerPredicate.Expand());

But I get the exact same exception.

The only difference in my code versus the documentation is that I dynamically build my nested predicate using a foreach loop. I just don't know how it can negatively affect the resulting expression.

  • What is wrong with my code ?

  • How can I actually debug this ?

  • Where the f parameter comes from ? How is it generated ? Why is it problematic in my case ?

  • Is there some kind of expression tree visualizer of some kind that could help me actually see what is wrong with the resulting expression ? Because the expression's body is hard to read.

Was it helpful?

Solution

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()
    .Where(mainPredicate)
    .Where(otherPredicate)
    .Where(yetAnotherPredicate)
    .ToList();

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top