定义为lambda表达式的LINQ表达式可以包含其他LINQ表达式吗? [英] Can a LINQ Expression defined as a lambda expression include other LINQ Expressions?

查看:168
本文介绍了定义为lambda表达式的LINQ表达式可以包含其他LINQ表达式吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用LINQ表达式时,C#编译器将方便地将C#lambda转换为Expression对象:

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

但是,在某些情况下,例如在运行时动态创建表达式时,有必要使用"longhand"表达式对象语法.因此,我目前发现自己混合使用了表达式lambda"和动态生成的显式"表达式对象.

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.

是否可以将Expression对象包含"或嵌入" lambda表达式中?

Is it possible to "include" or "embed" an Expression object into an expression lambda?

例如:

Expression inner_expression_object = Expression.Constant(3);

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

推荐答案

使用ExpressionVisitor,可以将Expression扩展方法的调用替换为Expression.

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

首先,您需要一个ExpressionVisitor类将Embed方法调用扩展为它们的值:

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

现在,您可以使用它们来扩展嵌入式的Expression值子表达式:

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;

您还可以直接嵌入Expression表达式并将其展开:

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;

这篇关于定义为lambda表达式的LINQ表达式可以包含其他LINQ表达式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆