title: Shared projection?
body: <p>I have a database and an Entity Framework model mapped to it. The database has a table "Products" with numerous columns. In many EF queries I need only a few columns, and I do a projection, let's say</p>
  
  <pre><code>var projected = Context.Products
      .Select(p =&gt; new ProjectedProduct { ProdID = p.ID, ProdTitle = p.Title })
      .ToArray();
  </code></pre>
  
  <p>Since this projection is used many times, I move it to a separate method:</p>
  
  <pre><code>public static IQueryable&lt;ProjectedProduct&gt; ToProjectedProduct(this IQueryable&lt;Product&gt; query)
  {
      return query.Select(p =&gt; 
          new ProjectedProduct { ProdID = p.ID, ProdTitle = p.Title });
  }
  </code></pre>
  
  <p>So I can use the projection like:  </p>
  
  <pre><code>var projected = Context.Products.ToProjectedProduct().ToArray();
  </code></pre>
  
  <p>Now I also want to use the same projection for a single instance of product, like:</p>
  
  <pre><code>var prod = Context.Products.First(p =&gt; p);
  var projected = new ProjectedProduct { ProdID = prod.ID, ProdTitle = prod.Title });
  </code></pre>
  
  <p>And I still want to use the same helper method for the projection in order to have it in one place, but it won't work, because it works only for IQueryable. What I can do is convert the projection to another method like</p>
  
  <pre><code>public static ProjectedProduct ToProjectedProduct(this Product p)
  {
      return new ProjectedProduct { ProdID = p.ID, ProdTitle = p.Title });
  }
  </code></pre>
  
  <p>But now this method won't work for IQueryable. I need a helper method which would work for both cases, what I would want to do is:</p>
  
  <pre><code>var projected = Context.Products.Select(p =&gt; p.ToProjectedProduct()).ToArray();
  </code></pre>
  
  <p>But this doesn't work because the helper method can't be translated to the database query.</p>