I have combined two expression with Expression.And. How can I compile this new expression with actual values?
var expr1 = Expression.Equal(Expression.Parameter(typeof(int), "param1"), Expression.Parameter(typeof(int), "param2"));
var expr2 = Expression.Equal(Expression.Parameter(typeof(int), "param3"), Expression.Parameter(typeof(int), "param4"));
var finalExpression = Expression.And(expr1, expr2);
I'm trying to get to work somoething like this after substiting the parameters with values
bool returnBool = Expression.Lambda<Func<bool>>(finalExpression).Compile()();
You need to compile it to a lambda with the same parameters:
var lambda = Expression.Lambda<Func<int, int, int, int, bool>>(
finalExpression, param1, ...).Compile();
lambda(1, 2, 3, 4);
Note that you need to pass the same Expression.Parameter()
instances used in the expression to Lambda()
.