title: Combine Lambda Expressions
body: <p>I am looking for a way to combine two lambda expressions, without using an <code>Expression.Invoke</code> on either expression. I want to essentially build a new expression that chains two separate ones. Consider the following code:</p>
  
  <pre><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; }
  }
  </code></pre>
  
  <p>And lets say I had two expressions:</p>
  
  <pre><code>Expression&lt;Func&lt;Model, Foo&gt;&gt; expression1 = m =&gt; m.SubModel.Foo;
  Expression&lt;Func&lt;Foo, string&gt;&gt; expression2 = f =&gt; f.Bar.Value;
  </code></pre>
  
  <p>And I want to join them together to functionally get the following expression:</p>
  
  <pre><code>Expression&lt;Func&lt;Model, string&gt;&gt; joinedExpression = m =&gt; m.SubModel.Foo.Bar.Value;
  </code></pre>
  
  <p>The only way I could think to do this is to use a ExpressionVisitor like this:</p>
  
  <pre><code>public class ExpressionExtender&lt;TModel, TIntermediate&gt; : ExpressionVisitor
  {
      private readonly Expression&lt;Func&lt;TModel, TIntermediate&gt;&gt; _baseExpression;
  
      public ExpressionExtender(Expression&lt;Func&lt;TModel, TIntermediate&gt;&gt; baseExpression)
      {
          _baseExpression = baseExpression;
      }
  
      protected override Expression VisitMember(MemberExpression node)
      {
          _memberNodes.Push(node.Member.Name);
          return base.VisitMember(node);
      }
  
      private Stack&lt;string&gt; _memberNodes;
  
      public Expression&lt;Func&lt;TModel, T&gt;&gt; Extend&lt;T&gt;(Expression&lt;Func&lt;TIntermediate, T&gt;&gt;  extend)
      {
          _memberNodes = new Stack&lt;string&gt;();
          base.Visit(extend);
          var propertyExpression  = _memberNodes.Aggregate(_baseExpression.Body, Expression.Property);
          return Expression.Lambda&lt;Func&lt;TModel, T&gt;&gt;(propertyExpression, _baseExpression.Parameters);
      }
  }
  </code></pre>
  
  <p>And then its used like this:</p>
  
  <pre><code>var expExt = new ExpressionExtender&lt;Model, Foo&gt;(expression1);
  var joinedExpression = expExt.Extend(expression2);
  </code></pre>
  
  <p>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.</p>
  
  <hr>
  
  <p>The <em>reason</em> 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 <code>MemberExpressions</code> 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 <code>Expression.Invoke()</code> 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.</p>