简体   繁体   English

在c#中创建表达式树

[英]Create an expression tree in c#

I am attempting to create a dynamic query using expression trees in LINQ to represent the following query 我试图在LINQ中使用表达式树创建一个动态查询来表示以下查询

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? 如何使用表达式树最好地构建上述查询?

The easiest way would be to just declare it as an Expression<Func<...>> 最简单的方法是将其声明为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 }
            )
        );
    }
}

Most people [that I've talked to] who enter the domain of expression trees are usually satisfied with the System.Linq.Dynamic functionality. 进入表达式树域的大多数[我已经与之交谈]的人通常对System.Linq.Dynamic功能感到满意。 (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. (这可以被滥用到很多不同的方式。)这个纯粹的awesomeness代码片段是Visual Studio示例代码的一部分,可能已经隐藏在你的硬盘驱动器的某个地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM