I have a list of expressions of type: Expression<Func<Person, bool>>
and I want to aggregate them and then compile the aggregated result into a single Func<Person, bool>
. I was able to create the aggregated expression but the part to compile the result aggregated expression throws an exception. Any help would be appreciated. Thank you.
Expression<Func<Person, bool>> expr1 = x => x.Age > 10;
Expression<Func<Person, bool>> expr2 = x => x.LastName == "some firstname";
Expression<Func<Person, bool>> expr3 = x => x.FirstName == "some lastname";
Expression<Func<Person, bool>> expr4 = x => x.Initial == 'a';
Expression<Func<Person, bool>> expr5 = x => x.DateOfBirth == DateTime.Now;
Expression<Func<Person, bool>> expr6 = x => x.Height > 10;
var exprList = new List<Expression<Func<Person, bool>>>()
{
expr1, expr2, expr3, expr4, expr5
};
var list = exprList
.Select(x => x.Body)
.Aggregate(Expression.AndAlso);
// this works, apparently?!
var aggregatedExpression = Expression.Lambda<Func<Person, bool>>(list, Expression.Parameter(typeof(Person), "x"));
// fails here! it cannot compile
var result = aggregatedExpression.Compile();
This is the exception:
Unhandled Exception: System.InvalidOperationException: variable 'x' of type 'TestAggregateExpression.Person' referenced from scope '', but it is not defined
at System.Linq.Expressions.Compiler.VariableBinder.Reference(ParameterExpression node, VariableStorageKind storage)