Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create an expression tree in c#

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?

like image 728
Waliaula Makokha Avatar asked Sep 08 '10 10:09

Waliaula Makokha


1 Answers

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

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.

like image 89
sisve Avatar answered Sep 30 '22 12:09

sisve