title: Perform .Select() on IQueryable with List of Func<T, string> delegates
body: <p>First of all, let me apologize in case the title doesn't make sense. I'm having a hard time understanding what I'm doing, let alone being able to use the right words for describing what I'm doing.</p>
  
  <p>I'm building a generic grid class in which I define the columns by a header / linq expression combination:</p>
  
  <pre><code>public class Column&lt;T&gt;
  {
      public string Header { get; set; }
      public Func&lt;T, string&gt; ValueExpression { get; set; }
  }
  </code></pre>
  
  <p>Usage:</p>
  
  <pre><code>Columns = new List&lt;Column&lt;Employee&gt;&gt;
                {
                    new Column&lt;Employee&gt; {Header = "Employee Id", ValueExpression = e =&gt; e.EmployeeID.ToString()},
                    new Column&lt;Employee&gt; {Header = "Name", ValueExpression = e =&gt; e.FirstName + " " + e.LastName},
                    new Column&lt;Employee&gt; {Header = "Employee Birthday Year", ValueExpression = e =&gt; e.BirthDate.HasValue ? e.BirthDate.Value.Year.ToString() : ""}
                },
  </code></pre>
  
  <p>I want to project the ValueExpression's (<code>Func&lt;T, string&gt;</code>) on an IQueryable 'employees':</p>
  
  <pre><code>var db = new NorthwindDataContext();
  var employees = db.Employees.Select(e =&gt; e);
  </code></pre>
  
  <p>I can get that to work while extracting a <code>IEnumerable&lt;IEnumerable&lt;string&gt;&gt;</code> from employees (a list of lists of strings used in my views) like this:</p>
  
  <pre><code>var elementsList = employees.ToPagedList(PageIndex, PageSize);
  var elementStringList = elementsList.ToStringList(Columns.Select(c =&gt; c.ValueExpression).ToArray());
  </code></pre>
  
  <p>Don't mind the PagedList stuff, it's irrelevant and kinda the same as ToList();</p>
  
  <p>Here's the ToStringList() Extension method:</p>
  
  <pre><code>public static IEnumerable&lt;IEnumerable&lt;string&gt;&gt; ToStringList&lt;T&gt;(this IPagedList&lt;T&gt; enumerable, params Func&lt;T, string&gt;[] fields)
  {
      foreach (var element in enumerable)
          yield return element.ToStringList(fields);
  }
  
  private static IEnumerable&lt;string&gt; ToStringList&lt;T&gt;(this T element, params Func&lt;T, string&gt;[] fields)
  {
      foreach (var field in fields)
          yield return field(element);
  }
  </code></pre>
  
  <p>The problem is that this approach involves executing the IQueryable before specifying the fields that have to be returned.</p>
  
  <p>As a result, the following query gets executed somewhere along the way:</p>
  
  <pre><code>SELECT [t0].[EmployeeID], [t0].[LastName], [t0].[FirstName], [t0].[Title], [t0].[TitleOfCourtesy], [t0].[BirthDate], [t0].[HireDate], [t0].[Address], [t0].[City], [t0].[Region], [t0].[PostalCode], [t0].[Country], [t0].[HomePhone], [t0].[Extension], [t0].[Photo], [t0].[Notes], [t0].[ReportsTo], [t0].[PhotoPath]
  FROM [dbo].[Employees] AS [t0]
  </code></pre>
  
  <p>As you might figure, it is undesired to retrieve ALL fields from the table Employees.</p>
  
  <p>I'm looking for a way to somehow build an extension method on the IQueryable employees in which I can pass a List of Func's (the "ValueExpression" members of Column) and "build" a new IQuerable that way, that will execute SQL that just retrieves the needed fields from the database.</p>
  
  <p><strong>So I'm looking for something like this:</strong></p>
  
  <pre><code>IQueryable employees = employees.SelectByExpressions(Columns.Select(c =&gt; c.ValueExpression).ToArray());
  </code></pre>
  
  <p>Thanks in advance.</p>