Create Expression Trees with a lambda expression
suggest changeThe following is the most basic expression tree created from a lambda.
Expression<Func<int, bool>> lambda = num => num == 42;
To build the same tree by hand, use the Expression class.
Expression above would be equivalent to:
ParameterExpression parameter = Expression.Parameter(typeof(int), "num"); // num argument
ConstantExpression constant = Expression.Constant(42, typeof(int)); // 42 constant
BinaryExpression equality = Expression.Equals(parameter, constant); // equality of two expressions (num == 42)
Expression<Func<int, bool>> lambda = Expression.Lambda<Func<int, bool>>(equality, parameter);
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents