title: Declare variable in LinQ expression tree
body: <p>In a previous question (<a href="https://stackoverflow.com/questions/20673086/refine-enumerable-linq-where-statement-with-repeated-parameters">Refine Enumerable LINQ WHERE statement with repeated parameters</a>), I solve the problem by declaring variable using <code>let</code> statement, and my code looks like this:</p>
  
  <pre><code>return
  from q in pList
  let currentContract = q.StaffContracts.OrderByDescending(p =&gt; p.SignedDate).Where(p =&gt; p.Active).FirstOrDefault()
  where (currentContract.Timespan &gt;= fromValue &amp;&amp; currentContract.Timespan &lt;= toValue)
  select q;
  </code></pre>
  
  <p>In this case, <code>currentContract</code> is the repeated expression. Now I have a new problem. My method doesn't provide nor request <code>IQueryable</code> anymore, but it requires an <code>Expression&lt;Func&lt;Staff, bool&gt;&gt;</code> as return instead (meaning, only the WHERE clause).</p>
  
  <p>I tried this but it is not successful:</p>
  
  <pre><code>return q =&gt;
  {
      var currentContract = q.StaffContracts.OrderByDescending(p =&gt; p.SignedDate).Where(p =&gt; p.Active).FirstOrDefault();
      return currentContract != null &amp;&amp; currentContract.Timespan &gt;= fromValue &amp;&amp; currentContract.Timespan &lt;= toValue;
  };
  </code></pre>
  
  <p>The compile error message is:</p>
  
  <blockquote>
    <p>A lambda expression with a statement body cannot be converted to an expression tree</p>
  </blockquote>
  
  <p>Is there any workaround with this?</p>