Can a LINQ Expression defined as a lambda expression include other LINQ Expressions?

流过昼夜 提交于 2019-12-11 08:54:42

问题


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();

回答1:


Using an ExpressionVisitor, you can replace calls to an Expression extension method with the Expression.

First, you need an ExpressionVisitor class to expand the Embed method calls to their values:

public class EmbedVisitor : ExpressionVisitor {
    public override Expression Visit(Expression node) {
        if (node?.NodeType == ExpressionType.Call) {
            var callnode = node as MethodCallExpression;
            if (callnode.Method.Name == "Embed" && callnode.Method.DeclaringType == typeof(ExpressionExt))
                return callnode.Arguments[0].Evaluate<Expression>();
        }

        return base.Visit(node);
    }
}

Then you need a static class for the extension methods needed:

public static class ExpressionExt {
    public static T Embed<T>(this Expression e) {
        return default(T);
    }

    public static Expression ExpandEmbed(this Expression orig) => new EmbedVisitor().Visit(orig);

    public static T Evaluate<T>(this Expression e) {
        //A little optimization for constant expressions
        if (e.NodeType == ExpressionType.Constant)
            return (T)((ConstantExpression)e).Value;
        else
            return (T)Expression.Lambda(e).Compile().DynamicInvoke();
    }
}

Now you can use these to expand embedded Expression valued sub-expressions:

var inner_expression_object = Expression.Constant(3);

Expression<Func<int, bool>> wrapper_expression =
    (int x) => x == inner_expression_object.Embed<int>();

var expanded = wrapper_expression.ExpandEmbed();
// Expression<Func<int,bool>> expanded == (int x) => x == 3;

You can also directly embed Expression expressions and expand them:

Expression<Func<int,bool>> wrap2 = x => x == Expression.Multiply(Expression.Constant(4), Expression.Constant(8)).Embed<int>();
var expanded2 = wrap2.ExpandEmbed();
// Expression<Func<int,bool>> expanded2 = x => x == 4*8;


来源:https://stackoverflow.com/questions/49918474/can-a-linq-expression-defined-as-a-lambda-expression-include-other-linq-expressi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!