title: Reuse select expressions in LINQ to Entities
body: <p>I have some complex business definitions that I'd like to define once and reuse them in Linq to Entities and also use them in other expressions that build upon it.</p>
  
  <p>My attempt is below, and this worked when I was originally passing the <code>ConvertOrderDetailsToViewModel</code> a <code>List</code>, but I want to perform this on an <code>IQueryable</code>, which results in the error:</p>
  
  <p><code>LINQ to Entities does not recognize the method 'System.Decimal Invoke(OrderDetail)' method, and this method cannot be translated into a store expression.</code> </p>
  
  <p>Is there a way to achieve this natively (without 3rd party libraries)?
  Looks like from <a href="https://stackoverflow.com/questions/29511860/reusable-functions-for-use-with-linq-to-entities">this answer</a>, you can define these functions in the database itself, and then call them from C#, but again, looking to do this solely in C# code if possible.</p>
  
  <p>Also came across <a href="https://stackoverflow.com/questions/986877/how-can-i-reuse-expressions-within-linq-statements">this answer</a> which looks like it works for a <code>Where</code> expression, but when I try to implement it for my <code>select</code> expression, I get the this error, which I get, because I am trying to assign the expression to a decimal.  </p>
  
  <p><code>Cannot implicitly convert type Expression&lt;Func&lt;OrderDetail, decimal&gt;&gt; to 'decimal'</code></p>
  
  <p>Here's the expression function:</p>
  
  <pre><code>public static Expression&lt;Func&lt;OrderDetail, decimal&gt;&gt; calcQtyNeedOD = (od) =&gt; (od.OrderedQuantity - od.PickedQuantity); 
  </code></pre>
  
  <p>View model creation method that calls this function:</p>
  
  <pre><code>public List&lt;AllocationNeedViewModel&gt; ConvertOrderDetailsToViewModel(IQueryable&lt;OrderDetail&gt; qryShipOrderDtls, List&lt;int&gt; itemIdsToExclude, bool boolGroupViewModelByItemAndRemnant)
  {
      var qryAllocNeedVMTEST = 
          from od in qryShipOrderDtls
          select new AllocationNeedViewModel()
          {
              WorkReleaseHeaderId = od.OrderHeader.WorkReleaseHeaderId.Value,
              OrderHeaderId = od.OrderHeaderId,
              OrderDetailId = od.Id,
              ItemId = od.ItemId.Value,
              ItemDescription = od.Item.Description,
              IsInventoryTracked = od.Item.TrackInventory,
              QtyNeed = calcQtyNeedOD(od),
              QtyRemain = 0,
              QtyAllocated = 0,
              IsAllocated = false
          }
          ;
          return qryAllocNeedVMTEST.ToList();
  }
  </code></pre>
  
  <p>To Make this more complex, there are other properties that I also want to have a reusable expression for, which would also use this first expression...i.e.</p>
  
  <pre><code>public static readonly Expression&lt;Func&lt;OrderDetail, decimal&gt;&gt; calcQtyRemainOD =
      (od) =&gt; calcQtyNeedOD.Compile().Invoke(od) - calcQtyAllocatedOD.Compile().Invoke(od); 
  </code></pre>
  
  <p><strong>UPDATE #1</strong></p>
  
  <p><strong>SEE UPDATE #2...this solution does NOT work!</strong></p>
  
  <p>Though so far no one has been able to provide a native way to reuse select expressions <em>across</em> queries, I did find a partial solution that works in being able to reuse them <em>within</em> the same query. Once you project/assign an expression to an entity's property, you can (unlike in T-SQL) then specify that property as part of another subsequent property expression.</p>
  
  <p>Example - this shows the full expressions for each property projection. In this example, <code>QtyRemain</code> is essentially just <code>QtyNeed - QtyAllocated</code>. I was re-specifying those again in the <code>QtyRemain</code> assignment: </p>
  
  <pre><code>public List&lt;AllocationNeedViewModel&gt; ConvertOrderDetailsToViewModel(IQueryable&lt;OrderDetail&gt; qryShipOrderDtls, List&lt;int&gt; itemIdsToExclude, bool boolGroupViewModelByItemAndRemnant)
  {
      var qryAllocNeedVM = qryShipOrderDtls
          .Select(od =&gt; new AllocationNeedViewModel() //Get all Work Order Detail Needs for Work Release
          {
              QtyNeed = (od.OrderedQuantity - od.PickedQuantity), 
              QtyAllocated = (od.AllocatedInventories.Count == 0 ? 0 : od.AllocatedInventories.Where(ai =&gt; ai.StatusId &lt; _AllocatedInventoryProcessedStatus).Sum(ai =&gt; ai.AllocatedQty)), 
              QtyRemain = (od.OrderedQuantity - od.PickedQuantity) - (od.AllocatedInventories.Count == 0 ? 0 : od.AllocatedInventories.Where(ai =&gt; ai.StatusId &lt; _AllocatedInventoryProcessedStatus).Sum(ai =&gt; ai.AllocatedQty))
          }
          );
          return qryAllocNeedVM.ToList();
  }
  </code></pre>
  
  <p>Instead, you can simply use the already defined properties in that <code>QtyRemain</code> property assignment, like so:</p>
  
  <pre><code>public List&lt;AllocationNeedViewModel&gt; ConvertOrderDetailsToViewModel(IQueryable&lt;OrderDetail&gt; qryShipOrderDtls, List&lt;int&gt; itemIdsToExclude, bool boolGroupViewModelByItemAndRemnant)
  {
      var qryAllocNeedVM = qryShipOrderDtls
          .Select(od =&gt; new AllocationNeedViewModel() //Get all Work Order Detail Needs for Work Release
          {
              QtyNeed = (od.OrderedQuantity - od.PickedQuantity), 
              QtyAllocated = (od.AllocatedInventories.Count == 0 ? 0 : od.AllocatedInventories.Where(ai =&gt; ai.StatusId &lt; _AllocatedInventoryProcessedStatus).Sum(ai =&gt; ai.AllocatedQty)), 
              QtyRemain = this.QtyNeed - this.QtyAllocated
          }
          );
          return qryAllocNeedVM.ToList();
  }
  </code></pre>
  
  <p>Though this is not a full solution to my original question, it is a partial solution that gets you some of the benefits desired.</p>
  
  <p><strong>UPDATE #2</strong></p>
  
  <p>I was wrong on UPDATE #1. Though this works and compiles and seems to generate SQL, it is not correct. The returned value of <code>this.QtyNeed</code> when being used in subsequent expressions always results to <code>0</code>. :( </p>