When using LINQ Expressions, the C# compiler will conveniently translate C# lambdas into Expression objects:
//using System;
//using System.Linq.Expressions;
Expression<Func<int, bool>> lambda_expression = (int x) => x == 3;
This is convenient, and can save a lot of typing versus explicitly constructing the expression:
Expression<Func<int, bool>> explicit_expression_object;
{
var x = Expression.Parameter(typeof(int), "x");
explicit_expression =
Expression.Lambda<Func<int, bool>>(Expression.Equal(x, Expression.Constant(3)), x);
}
However there are situations when it is necessary to use the "longhand" Expression object syntax, for example when dynamically creating expressions at run time. As such, I currently find myself using a mix of "expression lambdas" and dynamically generated "explicit" expression objects.
Is it possible to "include" or "embed" an Expression object into an expression lambda?
For example:
Expression inner_expression_object = Expression.Constant(3);
Expression<Func<int, bool>> wrapper_expression =
(int x) => x == inner_expression_object.Embed();