C#创建一个表达式树 [英] Create an expression tree in c#

查看:150
本文介绍了C#创建一个表达式树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用表达式树在LINQ代表下面的查询来创建一个动态查询

I am attempting to create a dynamic query using expression trees in LINQ to represent the following query

WageConstIns.Where(WageConstIn => WageConstIn.Serialno.ToString().StartsWith("2800"));



我试图像这样创造的:

I have attempted to create it like so:

MemberExpression le1 = LinqExpression.Property(paramExp, "Serialno");
MethodCallExpression le2 = LinqExpression.Call(le1, typeof(string).GetMethod("ToString",  System.Type.EmptyTypes));
ConstantExpression le3 = LinqExpression.Constant("2800");
MethodCallExpression le4 = LinqExpression.Call(le2, typeof(string).GetMethod("StartsWith"));



我在运行时得到一个错误。如何能在上面的查询最好使用表达式目录树建?

I am getting an error during runtime. How can the above query best be built using expression trees?

推荐答案

最简单的方法是只把它声明为一个表达式来; Func键< ...>>

The easiest way would be to just declare it as an Expression<Func<...>>

public static class Program {
    public static void Main() {
        Expression<Func<DummyClass, Boolean>> predicate = WageConstIn => WageConstIn.Serialno.ToString().StartsWith("2800");
    }
}



但是,如果你想用不同的表现方式来构建它。 ..

But if you want to construct it using different Expressions...

public static class Program {
    public static void Main() {
        var param = Expression.Parameter(typeof(DummyClass), "WageConstIn");
        var constValue = Expression.Constant("2800");

        // WageConstIn => WageConstIn.Serialno.ToString().StartsWith(...)
        var first = Expression.Lambda(
            parameters: param,
            body: Expression.Call(
                instance: Expression.Call(
                    instance: Expression.Property(param, "Serialno"),
                    methodName: "ToString",
                    typeArguments: null,
                    arguments: null
                ),
                methodName: "StartsWith",
                typeArguments: null,
                arguments: new[] { constValue }
            )
        );

        // WageConstIn => Convert.ToString(WageConstIn.Serialno).StartsWith(...)
        var second = Expression.Lambda(
            parameters: param,
            body: Expression.Call(
                instance: Expression.Call(
                    type: typeof(Convert),
                    methodName: "ToString",
                    typeArguments: null,
                    arguments: new[] { Expression.Property(param, "Serialno") }
                ),
                methodName: "StartsWith",
                typeArguments: null,
                arguments: new[] { constValue }
            )
        );
    }
}



大多数人[我已经说过了]谁输入表达式树的域名通常是满意的 System.Linq.Dynamic 功能。 (这可能会被滥用到了很多不同的方式。)这纯迷死代码片段是Visual Studio示例代码的一部分,很可能藏在某个地方在你的硬盘了。

Most people [that I've talked to] who enter the domain of expression trees are usually satisfied with the System.Linq.Dynamic functionality. (Which can be abused into a lot of different ways.) This code snippet of pure awesomeness is a part of the Visual Studio sample code, probably hiding somewhere on your harddrive already.

这篇关于C#创建一个表达式树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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