1

I've an Expression<Func<T,bool>> like this:

Expression<Func<T,bool>> filterExp = input => input.Deleted == false;

and then, I've a logic where another Expression<Func<T,bool>> is evaluated:

Expression<Func<T,bool>> standardExp = input => 1 == 1;
if (onlyStandard) {
    standardExp = input =>input.Standard == true;
}

I know that in this example, I could put this logic in the filterExp expression, but in my code standardExp is taken by a dictionary with type T as key, so I cannot put the logic in the filterExp or LinqToSQL could not translate it in a SQL Expression. What I'm trying to do, is merge these expressions in this way:

BinaryExpression exp = Expression.AndAlso(filterExp.Body, standardExp.Body);

Then, I create ParameterExpression with this statement:

var p = Expression.Parameter(typeof(T),"input");

And I create Lambda with this:

var lmb = Expression.Lambda<Func<T, bool>>(exp,new ParameterExpression[] { p });

But, when I'm trying to do this:

var result = db.Entities.Where(lmb);

I have an exception telling me that the input parameter is not binded. "The parameter 'input' was not bound in the specified LINQ to Entities query expression." Of course, in the generic scenario T was resolved with type "Entities". I'm new about Expressions tree, and I haven't found any documentation on parameter binding. What am I doing wrong? Please note again that the final lambda should be used with Entity Framework and should be convertible to SQL. Thank you.

Tatranskymedved
  • 4,194
  • 3
  • 21
  • 47
Sycraw
  • 543
  • 3
  • 17

1 Answers1

1

Found the response here:

Combining two expressions (Expression<Func<T, bool>>)

So basically, I was doing wrong here:

BinaryExpression exp = Expression.AndAlso(filterExp.Body, standardExp.Body);

Because I had to invoke those Expressions... but the invoke method is not EF-Save, except if you do an ExpressionVisitor for EF Compliance, exactly like shown in the link above.

Sycraw
  • 543
  • 3
  • 17