title: Querying a Single Column with LINQ
body: <p>I want to fetch array of values of a single column in a table, for example, I have a table named Customer(ID,Name), and want to fetch ids of all customers. my query in LINQ is:</p>
  
  <pre><code>var ids = db.Customers.Select(c=&gt;c.ID).ToList();
  </code></pre>
  
  <p>The answer of this query is correct, but I ran SQL Server Profiler, and saw the query which was like this:</p>
  
  <pre><code>SELECT [t0].[ID], [t0].[Name] FROM [dbo].[Customer] AS [t0]
  </code></pre>
  
  <p>I understood that LINQ selects all columns and then creates the integer array of ID fields.</p>
  
  <p>How can I write a LINQ query which generates this query in SQL Server:</p>
  
  <pre><code>SELECT [t0].[ID] FROM [dbo].[Customer] AS [t0]
  </code></pre>
  
  <p>Thank you.</p>
  
  <p>UPDATE: i have a function for doing this, and this function causes that result:</p>
  
  <pre><code>public static List&lt;TResult&gt; GetSingleColumn&lt;T, TResult&gt;(Func&lt;T, bool&gt; predicate, Func&lt;T, TResult&gt; select) where T : class
  {
      using (var db = new DBModelDataContext())
      {
          var q = db.GetTable&lt;T&gt;().AsQueryable();
          if (predicate != null)
              q = q.Where(predicate).AsQueryable();
          return q.Select(select).ToList();
      }
  }
  </code></pre>
  
  <p>and use it like this:</p>
  
  <pre><code>var ids = DBMH.GetSingleColumn&lt;Customer, int&gt;(null, c =&gt; c.ID);
  </code></pre>