34

I am learning expression trees in C#.

I am stuck now for a while:

string filterString = "ruby";
Expression<Func<string, bool>> expression = x => x == filterString;

How can I construct this expression by code? There is no sample how to capture a local variable. This one is easy:

Expression<Func<string, bool>> expression = x => x == "ruby";

This would be:

ParameterExpression stringParam = Expression.Parameter(typeof(string), "x");
Expression constant = Expression.Constant("ruby");
BinaryExpression equals = Expression.Equal(stringParam, constant);
Expression<Func<string, bool>> lambda1 =
    Expression.Lambda<Func<string, bool>>(
        equals,
        new ParameterExpression[] { stringParam });

The debugger prints the following for (x => x == filterString) :

{x => (x == value(Predicate.Program+<>c__DisplayClass3).filterString)}

Thanks for shedding some light on this topic.

3 Answers 3

42

Capturing a local variable is actually performed by "hoisting" the local variable into an instance variable of a compiler-generated class. The C# compiler creates a new instance of the extra class at the appropriate time, and changes any access to the local variable into an access of the instance variable in the relevant instance.

So the expression tree then needs to be a field access within the instance - and the instance itself is provided via a ConstantExpression.

The simplest approach for working how to create expression trees is usually to create something similar in a lambda expression, then look at the generated code in Reflector, turning the optimization level down so that Reflector doesn't convert it back to lambda expressions.

7
  • 2
    Thanks. The hint to look at the generated MSIL code is very helpful.
    – yonexbat
    Aug 28, 2011 at 18:00
  • 6
    Something like var hoistedConstant = Expression.Property(Expression.Constant(new {Value = filterString}), "Value"); should do it
    – Appetere
    Nov 17, 2015 at 11:19
  • 1
    @Appetere What about Expression.Constant(filterString)? Admittedly, it wouldn't reflect changes to the variable, but neither would your suggestion.
    – Zev Spitz
    Feb 9, 2017 at 7:58
  • Voted up, but Jame's answer was a little more on point for me.
    – Josh Mouch
    Apr 13, 2018 at 16:36
  • @Appetere this is the best answer in the entire here, thanks a lot Nov 16, 2021 at 12:40
10

This code wraps the expression in a closure Block that treats the local variable as a constant.

 string filterString = "ruby";

 var filterStringParam = Expression.Parameter(typeof(string), "filterString");
 var stringParam = Expression.Parameter(typeof(string), "x");

 var block = Expression.Block(
 // Add a local variable.
 new[] { filterStringParam },
 // Assign a constant to the local variable: filterStringParam = filterString
 Expression.Assign(filterStringParam, Expression.Constant(filterString, typeof(string))),
 // Compare the parameter to the local variable
 Expression.Equal(stringParam, filterStringParam));

 var x = Expression.Lambda<Func<string, bool>>(block, stringParam).Compile();
10

An old question but I came to it when trying to do something similar building expressions for Linq-to-entities (L2E) In that case you cannot use Expression.Block as it cannot be parsed down to SQL.

Here is an explicit example following Jon's answer which would work with L2E. Create a helper class to contain the value of the filter:

class ExpressionScopedVariables
{
    public String Value;
}

Build the tree thus:

var scope = new ExpressionScopedVariables { Value = filterString};
var filterStringExp = Expression.Constant(scope);
var getVariable = typeof(ExpressionScopedVariables).GetMember("Value")[0];
var access = Expression.MakeMemberAccess(filterStringExp, getVariable);

And then replace the constant in the original code with the member access expression:

BinaryExpression equals = Expression.Equal(stringParam, access);
Expression<Func<string, bool>> lambda1 =
    Expression.Lambda<Func<string, bool>>(
        equals,
        new ParameterExpression[] { stringParam });
2
  • This helped me a ton, thanks! My requirements were different, but the concept of creating a "scope" class, and creating an Expression from that got in the right direction.
    – Josh Mouch
    Apr 13, 2018 at 16:34
  • Thanks a lot ! This was really helpful
    – jbl
    Feb 7, 2019 at 16:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.