In .NET, an expression tree is a tree-like data structure, where each node is an expression, for example, a method call or a binary operation such as x < y
.
C# or Visual Basic compiler create an expression tree for based on an anonymous lambda expression, but you can create expression trees manually by using the System.Linq.Expressions
namespace.
When you assign a lambda expression to a variable of type Expression<TDelegate>
, the compiler emits code to build an expression tree that represents the lambda expression.
In this example, the C# compiler creates an expression tree that represents the lambda expression val => val < 10
.
Expression<Func<int, bool>> expression = val => val < 10;
var cResults = cExpression.Compile();
You can use the Expression class which contains static factory methods that create expression tree nodes of specific types. The following example manually builds the expression tree for the lambda expression val => val < 10
.
ParameterExpression numParam = Expression.Parameter(typeof(int), "val");
ConstantExpression ten = Expression.Constant(10, typeof(int));
BinaryExpression numLessThanTen = Expression.LessThan(numParam, ten);
Expression<Func<int, bool>> expression =
Expression.Lambda<Func<int, bool>>(
numLessThanTen,
new ParameterExpression[] { numParam });
var eResults = expression.Compile();