title: Is there a particular reason LinqKit's expander can't pick up Expressions from fields?
body: <p>I'm using <a href="http://www.albahari.com/nutshell/linqkit.aspx">LinqKit</a> library which allows combining expressions on the fly.  </p>
  
  <p>This is a pure bliss for writing Entity Framewok data acess layer because several expressions can optionally be reused and combined, which allows both for readable and efficient code.</p>
  
  <p>Consider following piece of code:</p>
  
  <pre><code>private static readonly Expression&lt;Func&lt;Message, int, MessageView&gt;&gt; _selectMessageViewExpr =
      ( Message msg, int requestingUserId ) =&gt;
          new MessageView
          {
              MessageID = msg.ID,
              RequestingUserID = requestingUserId,
              Body = ( msg.RootMessage == null ) ? msg.Body : msg.RootMessage.Body,
              Title = ( ( msg.RootMessage == null ) ? msg.Title : msg.RootMessage.Title ) ?? string.Empty
          };
  </code></pre>
  
  <p>We declare an expression that projects <code>Message</code> onto <code>MessageView</code> (I removed the details for clarity).  </p>
  
  <p>Now, the data access code can use this expression to get individual message:</p>
  
  <pre><code>var query = CompiledQueryCache.Instance.GetCompiledQuery(
      "GetMessageView",
      () =&gt; CompiledQuery.Compile(
          _getMessagesExpr
              .Select( msg =&gt; _selectMessageViewExpr.Invoke( msg, userId ) ) // re-use the expression
              .FirstOrDefault( ( MessageView mv, int id ) =&gt; mv.MessageID == id )
              .Expand()
          )
      );
  </code></pre>
  
  <p>This is beautiful because the very same expression can be reused for getting a message list as well:</p>
  
  <pre><code>var query = CompiledQueryCache.Instance.GetCompiledQuery(
      "GetMessageViewList",
      () =&gt; CompiledQuery.Compile(
          BuildFolderExpr( folder )
              .Select( msg =&gt; _selectMessageViewExpr.Invoke( msg, userId ) )
              .OrderBy( mv =&gt; mv.DateCreated, SortDirection.Descending )
              .Paging()
              .Expand()
          ),
      folder
      );
  </code></pre>
  
  <p>As you can see, projection expression is stored in <code>_selectMessageViewExpr</code> and is used for building several different queries.</p>
  
  <p>However, I spent a lot of time tracing a strange error where <strong>this code crashed at <code>Expand()</code> call</strong>.<br>
  The error said:</p>
  
  <blockquote>
    <p>Unable to cast object of type <code>System.Linq.Expressions.FieldExpression</code> to type <code>System.Linq.Expressions.LambdaExpression</code>.</p>
  </blockquote>
  
  <p>It's only after a while that I realized that <strong>everything works when expression is referenced in a local variable before being called <code>Invoke</code> on</strong>:</p>
  
  <pre><code>var selector = _selectMessageViewExpr; // reference the field
  
  var query = CompiledQueryCache.Instance.GetCompiledQuery(
      "GetMessageView",
      () =&gt; CompiledQuery.Compile(
          _getMessagesExpr
              .Select( msg =&gt; selector.Invoke( msg, userId ) ) // use the variable
              .FirstOrDefault( ( MessageView mv, int id ) =&gt; mv.MessageID == id )
              .Expand()
          )
      );
  </code></pre>
  
  <p><em>This</em> code works as expected.</p>
  
  <p>My question is:</p>
  
  <blockquote>
    <p><strong>Is there any specific reason why LinqKit doesn't recognize <code>Invoke</code> on expressions stored in fields?</strong> Is it just an omission by developer, or is there some important reason why expressions need to be stored in local variables first?</p>
  </blockquote>
  
  <p>This question can probably be answered by looking at generated code and checking LinqKit sources, however I thought maybe someone related to LinqKit development could answer this question.</p>
  
  <p>Thanks.</p>