How I do extend an linq expression whilst keeping it an expression? I've simplified this quite a bit (to avoid pasting pages) - .e.g I working with Queryable rather than Enumerable, but the solution for this will suffice, ultimately I need to keep it as an expression whilst adding a method call to it.
For exampleL
var p1 = new Person() {Name = "RDA1", Age = 27};
var p2 = new Person() {Name = "RDA2", Age = 28};
var p3 = new Person() {Name = "RDA3", Age = 29};
var people = new[] {p1, p2, p3};
Expression<Func<IEnumerable<Person>, IEnumerable<Person>>> filterExp
= list => list.Take(2);
Expression<Func<Person, int>> sortExp = l => l.Age;
MethodCallExpression orderByCallExpression = Expression.Call(
typeof (Enumerable),
"OrderByDescending",
new Type[] {typeof (Person), typeof (int)},
filterExp.Parameters[0],
sortExp);
var combinedExpression = Expression.Lambda<Func<IEnumerable<Person>, IEnumerable<Person>>>
(filterExp.AddMethodCall(orderByCallExpression)); // made up AddMethodCall but you get the idea
I've searched dozens of SO posts for the past few hours and I can't seem to figure this out, I can do it if I compile filterExp but not without keeping both expressions and end result an expression.