Why are some object properties UnaryExpression and others MemberExpression?

I think I know what the problem is. Your expression returns type object. If you change this to Expression<Func<T, R>> the return type should be correctly inferred, and UnaryExpression (which I will assume is some boxing operation) should not occur. Update: The signature for Include should be: public void Include<T, R>(Expression<Func<T, R>> expression)

How do I dynamically create an Expression predicate from Expression?

It’s hard to mix compiler-generated expression trees and hand-made ones, precisely because of this sort of thing – extracting out the ParameterExpressions is tricky. So let’s start from scratch: ParameterExpression argParam = Expression.Parameter(typeof(Service), “s”); Expression nameProperty = Expression.Property(argParam, “Name”); Expression namespaceProperty = Expression.Property(argParam, “Namespace”); var val1 = Expression.Constant(“Modules”); var val2 = Expression.Constant(“Namespace”); Expression e1 = … Read more

Convert Linq expression “obj => obj.Prop” into “parent => parent.obj.Prop”

What you’re looking for is the ability to compose expressions, just as you can compose functions: public static Expression<Func<T, TResult>> Compose<T, TIntermediate, TResult>( this Expression<Func<T, TIntermediate>> first, Expression<Func<TIntermediate, TResult>> second) { return Expression.Lambda<Func<T, TResult>>( second.Body.Replace(second.Parameters[0], first.Body), first.Parameters[0]); } This relies on the following method to replace all instances of one expression with another: public class … Read more