Partial Evaluation of ConstantExpression in ExpressionTreeVisitor

Posted by Andrew Theken on Stack Overflow See other posts from Stack Overflow or by Andrew Theken
Published on 2010-03-27T16:21:12Z Indexed on 2010/03/27 16:23 UTC
Read the original article Hit count: 322

Filed under:
|
|
|
|

Hi all, I am not an Expression Tree master by any stretch of the imagination, what I have is code that looks like this:

    int external = 10;
    using(var session = new Session())
    {
       session.Add(new Product { Name = "test1", Price = 20 });
       session.Add(new Product {Name = "test", Price = 10});
       var product = session.Products.Where(p => p.Price == external).FirstOrDefault();
       Assert.Equal(10, product.Price);
    }

Session implements all the IQueryProvider, IQueryable interfaces you'd come to expect from a LINQ Provider.

When I execute the query, everything goes to plan until I read the ConstantExpression for "external", at which point, I am at a loss as to how to move forward because:

      //constant is ConstantExpression for "external" on the right side of the "p.Price == external" expression above.
      var t = constant.GetType(); //evaluates to class called "<>c__DisplayClass2" - with a member named "external" that has the value 10. 

The question is basically.. How can I just access the value of the member "external" - is there a way to accomplish this without using reflection? or am I cooked? What am I missing?

© Stack Overflow or respective owner

Related posts about c#

Related posts about expressiontrees