Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the result from an Expression

I've created a lambda expression at runtime, and want to evaluate it - how do I do that? I just want to run the expression by itself, not against any collection or other values.

At this stage, once it's created, I can see that it is of type Expression<Func<bool>>, with a value of {() => "MyValue".StartsWith("MyV")}.

I thought at that point I could just call var result = Expression.Invoke(expr, null); against it, and I'd have my boolean result. But that just returns an InvocationExpression, which in the debugger looks like {Invoke(() => "MyValue".StartsWith("MyV"))}.

I'm pretty sure I'm close, but can't figure out how to get my result!

Thanks.

like image 708
LJW Avatar asked Dec 06 '09 18:12

LJW


3 Answers

Try compiling the expression with the Compile method then invoking the delegate that is returned:

using System;
using System.Linq.Expressions;

class Example
{
    static void Main()
    {
        Expression<Func<Boolean>> expression 
                = () => "MyValue".StartsWith("MyV");
        Func<Boolean> func = expression.Compile();
        Boolean result = func();
    }
}
like image 114
Andrew Hare Avatar answered Nov 04 '22 22:11

Andrew Hare


As Andrew mentioned, you have to compile an Expression before you can execute it. The other option is to not use an Expression at all, which woul dlook like this:

Func<Boolean> MyLambda = () => "MyValue".StartsWith("MyV");
var Result = MyLambda();

In this example, the lambda expression is compiled when you build your project, instead of being transformed into an expression tree. If you are not dynamically manipulating expression trees or using a library that uses expression trees (Linq to Sql, Linq to Entities, etc), then it can make more sense to do it this way.

like image 2
Chris Pitman Avatar answered Nov 04 '22 20:11

Chris Pitman


The way I would do it is lifted right from here: MSDN example

delegate int del(int i);
static void Main(string[] args)
{
    del myDelegate = x => x * x;
    int j = myDelegate(5); //j = 25
}

Also if you want to use an Expression<TDelegate> type then this page: Expression(TDelegate) Class (System.Linq.Expression) has the following example:

// Lambda expression as executable code.
Func<int, bool> deleg = i => i < 5;
// Invoke the delegate and display the output.
Console.WriteLine("deleg(4) = {0}", deleg(4));

// Lambda expression as data in the form of an expression tree.
System.Linq.Expressions.Expression<Func<int, bool>> expr = i => i < 5;
// Compile the expression tree into executable code.
Func<int, bool> deleg2 = expr.Compile();
// Invoke the method and print the output.
Console.WriteLine("deleg2(4) = {0}", deleg2(4));

/*  This code produces the following output:
    deleg(4) = True
    deleg2(4) = True
*/
like image 1
Matt Ellen Avatar answered Nov 04 '22 20:11

Matt Ellen