10

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 that chains two separate ones. Consider the following code:

class Model {
    public SubModel SubModel { get; set;}
}

class SubModel {
    public Foo Foo { get; set; }
}

class Foo {
    public Bar Bar { get; set; }
}

class Bar {
    public string Value { get; set; }
}

And lets say I had two expressions:

Expression<Func<Model, Foo>> expression1 = m => m.SubModel.Foo;
Expression<Func<Foo, string>> expression2 = f => f.Bar.Value;

And I want to join them together to functionally get the following expression:

Expression<Func<Model, string>> joinedExpression = m => m.SubModel.Foo.Bar.Value;

The only way I could think to do this is to use a ExpressionVisitor like this:

public class ExpressionExtender<TModel, TIntermediate> : ExpressionVisitor
{
    private readonly Expression<Func<TModel, TIntermediate>> _baseExpression;

    public ExpressionExtender(Expression<Func<TModel, TIntermediate>> baseExpression)
    {
        _baseExpression = baseExpression;
    }

    protected override Expression VisitMember(MemberExpression node)
    {
        _memberNodes.Push(node.Member.Name);
        return base.VisitMember(node);
    }

    private Stack<string> _memberNodes;

    public Expression<Func<TModel, T>> Extend<T>(Expression<Func<TIntermediate, T>>  extend)
    {
        _memberNodes = new Stack<string>();
        base.Visit(extend);
        var propertyExpression  = _memberNodes.Aggregate(_baseExpression.Body, Expression.Property);
        return Expression.Lambda<Func<TModel, T>>(propertyExpression, _baseExpression.Parameters);
    }
}

And then its used like this:

var expExt = new ExpressionExtender<Model, Foo>(expression1);
var joinedExpression = expExt.Extend(expression2);

It works, but it feels a bit clunky to me. I'm still trying to wrap my head expressions and wondering if there is a more idiomatic way to express this, and I have the sneaky suspicion that I missing something obvious.


The reason I want to do this is to use it with the ASP.net mvc 3 Html helpers. I have some deeply nested ViewModels and some HtmlHelper extensions that help deal with those, so the expression needs to be just a collection of MemberExpressions for the built in MVC helpers to process them correctly and build the correctly deeply nested name attribute values. My first instinct was to use Expression.Invoke() and invoke the first expression and chain it to the second, but the MVC helpers didn't like that very much. It lost its hierarchical context.

J. Holmes
  • 18,466
  • 5
  • 47
  • 52

3 Answers3

21

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);
    }
}
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • +1 This makes a lot of sense now that I see it. One thing I failed to mention in the original question: is there a way to do this without mutating either starting expression. For example, I have a core expression that i need to extend in several different ways, generating new expressions with each invocation. – J. Holmes Feb 03 '12 at 18:00
  • @32bitkid yes! expression is immutable; I have not mutated either of them! – Marc Gravell Feb 03 '12 at 18:14
6

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.

Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067
  • @Kobi: I don't understand the question. Is *what* still possible? And `a` and `b` are not lambdas; they are formal parameters. – Eric Lippert Feb 03 '12 at 17:40
  • Is this solution suitable for two `Expression`s, or just for two `Func<>`s? (ok, never mind - I think I understand your answer now) – Kobi Feb 03 '12 at 17:41
  • @Kobi: I don't understand what you mean by "suitable". Suppose you have the expression for the constant 1 and the expression for the constant 2. What operation do you wish to perform on these two expressions that is analogous to function composition on lambdas? – Eric Lippert Feb 03 '12 at 17:48
  • The context of the question talked about the issue of MVC needing inlined; would this approach achieve this? – Marc Gravell Feb 03 '12 at 18:18
0

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"
    }
}
SeanJ_2173
  • 35
  • 6