When creating binary lambda expressions, the lambda parameter is not in scope.

c# expression-trees lambda


When creating a lambda expression by hand I get a 'Parameter not in scope' exception.

Any ideas as to what I am doing wrong?

 public class OtherType
        public string First_Name { get; set; }
        public string Last_Name { get; set; }

    static void Main(string[] args)

          Expression<Func<OtherType, bool>> l2 = 
                p => p.First_Name == "Bob";
            l2.Compile();  // Works 

            PropertyInfo property = 

            ParameterExpression para = 
                Expression.Parameter(typeof(OtherType), "para");

            ConstantExpression right = 
                Expression.Constant("Bob", typeof(string));

            MemberExpression left = 
                Expression.Property(Expression.Parameter(typeof(OtherType), "para"), property);

            BinaryExpression binary = 
                Expression.MakeBinary(ExpressionType.Equal, left, right);

            Expression<Func<OtherType, bool>> l = 
                Expression.Lambda<Func<OtherType, bool>>(binary, new ParameterExpression[] { para });

            l.Compile(); // Get a 'Lambda Parameter not in scope' exception

10/15/2009 7:12:36 PM

Accepted Answer

You need to reuse the same parameter object. So where you've got:

 MemberExpression left = Expression.Property
     (Expression.Parameter(typeof(OtherType), "para"), property);

it should be:

  MemberExpression left = Expression.Property(para, property);

I know it would make sense for them to match by name, but that's just not the way it works :(

If it's any consolation at all, I very rarely get hand-built expression trees right first time. I have to swear at them for a while. On the other hand, I believe that on cold enough days, Marc Gravell can exhale carefully and his breath will come out in the form of perfect, frosty expression tree code...

10/15/2009 7:16:23 PM

Related Questions

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow