C# Language Create Expression Trees with a lambda expression


Following is most basic expression tree that is created by lambda.

Expression<Func<int, bool>> lambda = num => num == 42;

To create expression trees 'by hand', one should use 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);