title: Using Expression<Func<T, TU>> within another Expression<Func<T, TU>>
body: <p>I'm trying to get my head around combining expressions, looking around it should be possible to do what I'm trying to do but I'm struggling. </p>
  
  <p>I have an <code>Expression&lt;Func&lt;class, dynamic&gt;&gt;</code> which defines my selection that I eventually run against my EF context, where <code>class</code> is a table I'm selecting data from. For example:</p>
  
  <pre><code>Expression&lt;Func&lt;foo, dynamic&gt;&gt; expression = foo =&gt; new
  {
     RecordExists = foo.bar.Exists,
     RecordHasPaid = foo.bar.Amount != null &amp;&amp; foo.bar.Amount &gt; 0
  }
  
  // run the expression against the database and get the results...
  var results = context.foo.Select(expression);
  </code></pre>
  
  <p>What I want to be able to do is to then extract common logic within that expression out into static "helper" style expressions I can re-use, for example, rather than having this line within my first expression </p>
  
  <p><code>RecordHasPaid = foo.bar.Amount != null &amp;&amp; foo.bar.Amount &gt; 0</code> </p>
  
  <p>I could have a re-usable expression, stored in a re-usable class, say <code>ExpressionHelper</code>, that I can then insert into my first expression:</p>
  
  <pre><code>public static Expression&lt;Func&lt;bar, bool&gt;&gt; BarHasPaid(bar foobar)
  {
     return b =&gt; b.Amount != null &amp;&amp; b.Amount &gt; 0;
  }
  </code></pre>
  
  <p>Becomes:</p>
  
  <pre><code>Expression&lt;Func&lt;foo, dynamic&gt;&gt; expression = foo =&gt; new
  {
     RecordExists = foo.bar.Exists,
     RecordHasPaid = ExpressionHelper.BarHasPaid(foo.bar)
  }
  
  // run the expression against the database and get the results...
  var results = context.foo.Select(expression);
  </code></pre>
  
  <p>Note that I am passing in a different parameter to this expression than my outer expression (hopefully I can be that generic). </p>
  
  <p>Sounds great, compiles fine, however - when I come to run the .Select I get the dreaded error:</p>
  
  <blockquote>
    <p>LINQ to Entities does not recognize the method
    'System.Linq.Expressions.Expression1[System.Func2[bar,System.Boolean]]
    BarHasPaid(bar)' method, and this method cannot be translated into a
    store expression.</p>
  </blockquote>
  
  <p>I understand the reason why this happens (at least I think I do) in that my method is not known so cannot be translated into SQL, and additionally my expression cannot be turned into an Expression Tree which makes sense to EF because I am essentially using two different expressions within each other and it doesn't know how to turn them into something that makes sense.</p>
  
  <p>I've tried various things, combining the expressions such as <a href="https://stackoverflow.com/questions/1717444/combining-two-lambda-expressions-in-c-sharp">here</a> which unfortunately seems to work when you have two expressions, not one expression <strong>inside</strong> another expression.</p>
  
  <p>I've also tried implementing the wrap/unwrap with custom ExpressionVisitors as mentioned <a href="https://stackoverflow.com/questions/29448432/pass-expression-parameter-as-argument-to-another-expression/29471092#29471092">here</a> (I did eventually get this to compile fine, but I get the same error)</p>
  
  <p>I've also tried many other similar answers and tried using LinqKit to <code>Expand</code> my expression and also set my context with <code>AsExpandable</code> to no avail. All of my attempts so far have resulted in either the above error, or this one when trying to combine using <code>LambdaExpression.Lambda</code> to compile my combined expression:</p>
  
  <blockquote>
    <p>variable 'foo' of type referenced from scope '' but it is not defined</p>
  </blockquote>
  
  <p>Which I believe is being thrown because the variable <code>foo</code> which is used within my outer expression, <code>Expression&lt;Func&lt;foo, dynamic&gt;&gt;</code>, does not exist within my expression <code>Expression&lt;Func&lt;bar, bool&gt;&gt;</code> because the resulting expression when they are combined would make no sense.</p>
  
  <p>Unfortunately I don't think I have a deep enough understanding of exactly what it is I'm trying to do, so I've hit a bit of a dead end as to where to turn next. Any suggestions as to if this is possible and/or how to achieve it would be greatly appreciated.</p>
  
  <p>EDIT: I have created two sample projects using Rextester, <a href="http://rextester.com/GKQCI31771" rel="nofollow noreferrer">this is the working example</a> and <a href="http://rextester.com/NCYQ7389" rel="nofollow noreferrer">this is what causes the error</a> - using this online tool you can actually see what EF should be running but as there is no EF to run it against the error doesn't actually occur during that test.</p>