I am looking for a way to combine two lambda expressions, without using an Expression.Invoke
on either expression. I want to essentially build a new expression
Use a visitor to swap all instances of the parameter f
to m.SubModel.Foo
, and create a new expression with m
as the parameter:
internal static class Program
{
static void Main()
{
Expression<Func<Model, Foo>> expression1 = m => m.SubModel.Foo;
Expression<Func<Foo, string>> expression2 = f => f.Bar.Value;
var swap = new SwapVisitor(expression2.Parameters[0], expression1.Body);
var lambda = Expression.Lambda<Func<Model, string>>(
swap.Visit(expression2.Body), expression1.Parameters);
// test it worked
var func = lambda.Compile();
Model test = new Model {SubModel = new SubModel {Foo = new Foo {
Bar = new Bar { Value = "abc"}}}};
Console.WriteLine(func(test)); // "abc"
}
}
class SwapVisitor : ExpressionVisitor
{
private readonly Expression from, to;
public SwapVisitor(Expression from, Expression to)
{
this.from = from;
this.to = to;
}
public override Expression Visit(Expression node)
{
return node == from ? to : base.Visit(node);
}
}
Your solution seems to be narrowly tailored to your specific problem, which seems inflexible.
It seems to me that you could solve your problem straightforwardly enough through simple lambda substitution: replace instances of the parameter (or "free variable" as they call it in lambda calculus) with the body. (See Marc's answer for some code to do so.)
Since parameter in expression trees have referential identity rather than value identity there isn't even a need to alpha rename them.
That is, you have:
Expression<Func<A, B>> ab = a => f(a); // could be *any* expression using a
Expression<Func<B, C>> bc = b => g(b); // could be *any* expression using b
and you wish to produce the composition
Expression<Func<A, C>> ac = a => g(f(a)); // replace all b with f(a).
So take the body g(b)
, do a search-and-replace visitor looking for the ParameterExpression
for b
, and replace it with the body f(a)
to give you the new body g(f(a))
. Then make a new lambda with the parameter a
that has that body.
Update: the below answer generates an "Invoke" which EF does not support.
I know this is an old thread, but I have the same need and I figured out a cleaner way to do it. Assuming that you can alter your "expression2" to user a generic lambda, you can inject one like this:
class Program
{
private static Expression<Func<T, string>> GetValueFromFoo<T>(Func<T, Foo> getFoo)
{
return t => getFoo(t).Bar.Value;
}
static void Main()
{
Expression<Func<Model, string>> getValueFromBar = GetValueFromFoo<Model>(m => m.SubModel.Foo);
// test it worked
var func = getValueFromBar.Compile();
Model test = new Model
{
SubModel = new SubModel
{
Foo = new Foo
{
Bar = new Bar { Value = "abc" }
}
}
};
Console.WriteLine(func(test)); // "abc"
}
}