4

Ok, I am sure this is simple, but I am having a senior moment.

I have a simple BinaryExpression (greaterthan) the left side is a ParameterExpression and the right side is a ConstantExpression I want to compile this expression to a func that I can call and pass a parameter to...

var func = ...something with my exp....

bool result = func(myValue);

Thanks to Hasan, I modified his answer to my needs...

var func = Expression.Lambda<Func<int,bool>>(myExpr, (ParameterExpression)myExpr.left).Compile();

1 Answer 1

7
var param = Expression.Parameter(typeof(int));
var value = Expression.Constant(3);
var body = Expression.GreaterThan(param, value);
var checkValue = Expression.Lambda<Func<int, bool>>(body, param).Compile();

Console.WriteLine(checkValue(4));
Console.WriteLine(checkValue(2));
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. In my case I already have the BinaryExpression so I didn't need to create it....but what I was missing is the requirement to pass the param into the lambda method....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.