title: How to assign a property value of an IQueryable<T>?
body: <p>I'm using Entity Framework 4.1 Code First.  In my entity, I have three date/time properties:</p>
  
  <pre><code>public class MyEntity
  {
      [Key]
      public Id { get; set; }
  
      public DateTime FromDate { get; set; }
  
      public DateTime ToDate { get; set; }
  
      [NotMapped]
      public DateTime? QueryDate { get; set; }
  
      // and some other fields, of course
  }
  </code></pre>
  
  <p>In the database, I always have the From/To dates populated.  I query against them using a simple where clause.  But in the result set, I want to include the date I queried for. I need to persist this for some other business logic to work.</p>
  
  <p>I'm working on an extension method to do this, but I'm running into problems:</p>
  
  <pre><code>public static IQueryable&lt;T&gt; WhereDateInRange&lt;T&gt;(this IQueryable&lt;T&gt; queryable, DateTime queryDate) where T : MyEntity
  {
      // this part works fine
      var newQueryable = queryable.Where(e =&gt; e.FromDate &lt;= queryDate &amp;&amp;
                                              e.ToDate &gt;= queryDate);
  
      // in theory, this is what I want to do
      newQueryable = newQueryable.Select(e =&gt;
                                             {
                                                 e.QueryDate = queryDate;
                                                 return e;
                                             });
      return newQueryable;
  }
  </code></pre>
  
  <p>This doesn't work.  It works if I use an IEnumerable, but I want to keep it as IQueryable so everything runs on the database side, and this extention method can still be used in any part of another query.  When it's IQueryable, I get a compile error of the following:</p>
  
  <blockquote>
    <p>A lambda expression with a statement body cannot be converted to an expression tree</p>
  </blockquote>
  
  <p>If this was SQL, I would just do something like this:</p>
  
  <pre><code>SELECT *, @QueryDate as QueryDate
  FROM MyEntities
  WHERE @QueryDate BETWEEN FromDate AND ToDate
  </code></pre>
  
  <p>So the question is, how can I transform the expression tree I already have to include this extra property assignment?  I have looked into IQueryable.Expression and IQueryable.Provider.CreateQuery - there's a solution in there somewhere.  Maybe an assignment expression can be appended to the existing expression tree?  I'm not familiar enough with the expression tree methods to figure this out.  Any ideas?</p>
  
  <p><strong>Example Usage</strong></p>
  
  <p>To clarify, the goal is to be able to perform something like this:</p>
  
  <pre><code>var entity = dataContext.Set&lt;MyEntity&gt;()
                          .WhereDateInRange(DateTime.Now)
                          .FirstOrDefault();
  </code></pre>
  
  <p>And have the DateTime.Now persisited into the QueryDate of the resulting row, WITHOUT having more than one row returned from the database query.  (With the IEnumerable solution, multiple rows are returned before FirstOrDefault picks the row we want.)</p>
  
  <p><strong>Another Idea</strong></p>
  
  <p>I could go ahead and map QueryDate like a real field, and set its DatabaseGeneratedOption to Computed.  But then I would need some way to inject the "@QueryDate as QueryDate" into the SQL created by EF's select statements.  Since it's computed, EF won't try to provide values during update or insert.  So how could I go about injecting custom SQL into the select statements?</p>