title: LINQ to SQL Projection: Func vs Inline
body: <p>I am finding some unexpected behavior when using a projection in a LINQ to SQL query using a Func. Example code will explain better than words.</p>
  
  <p>A basic L2S lambda query using projection:</p>
  
  <pre><code>db.Entities.Select(e =&gt; new DTO(e.Value));
  </code></pre>
  
  <p>It translates to the desired SQL:</p>
  
  <pre><code>SELECT [t1].[Value]
  FROM [Entity] AS [t1]
  </code></pre>
  
  <p>However, when the projection is put into a Func like this:</p>
  
  <pre><code>Func&lt;Entity, DTO&gt; ToDTO = (e) =&gt; new DTO(e.Value);
  </code></pre>
  
  <p>And called like this:</p>
  
  <pre><code>db.Entities.Select(e =&gt; ToDTO(e));
  </code></pre>
  
  <p>The SQL is now pulling back all of the columns in the table, not just the one in the projection:</p>
  
  <pre><code>SELECT [t1].[Id], [t1].[Value], [t1].[timestamp], [t1].[etc...]
  FROM [Entity] AS [t1]
  </code></pre>
  
  <p>So my question is, how do I encapsulate this projection without the LINQ to SQL instantiating the whole Entity?</p>
  
  <p>Things to keep in mind, the DTO I am using has a protected default constructor, so I can't use an object initializer. And since the DTO class cannot be modified, I'd have to make a subclass to implement that behavior. Which is fine, if that's the only solution.</p>
  
  <p>Thanks.</p>
  
  <p>Edit:</p>
  
  <p>Thanks to Brian for the solution. I had previously tried an Expression but couldn't figure out the syntax. Here's the working code:</p>
  
  <pre><code>Expression&lt;Entity, DTO&gt; ToDTO = (e) =&gt; new DTO(e.Value);
  </code></pre>
  
  <p>Then call it like this:</p>
  
  <pre><code>db.Entities.Select(ToDTO);
  </code></pre>
  
  <p>At first I was trying to call it like this, which wouldn't compile. This is the proper syntax for calling a Func, but not an Expression.</p>
  
  <pre><code>db.Entities.Select(e =&gt; ToDTO(e));
  </code></pre>