Search code examples
linqc#-4.0expression-trees

Building a lambda with an Expression Tree


I'm having a hard time grasping the expression trees. I would like to be able to build an expression tree manually for the following statement:

c => c.Property

A lot of the tutorials focus around comparing, while I just want it to return this one property. Any help?


Solution

  • ParameterExpression parameter = Expression.Parameter(typeof(YourClass), "c");
    Expression property = Expression.PropertyOrField(parameter, "Property");
    Expression<Func<YourClass, PropertyType>> lamda = Expression.Lambda<Func<YourClass, PropertyType>>(property, parameter);