Querying a Single Column with LINQ
Posted
by Hossein Margani
on Stack Overflow
See other posts from Stack Overflow
or by Hossein Margani
Published on 2010-05-26T06:15:12Z
Indexed on
2010/05/26
6:31 UTC
Read the original article
Hit count: 315
Hi Every one!
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:
var ids = db.Customers.Select(c=>c.ID).ToList();
The answer of this query is correct, but I ran SQL Server Profiler, and saw the query which was like this:
SELECT [t0].[ID], [t0].[Name] FROM [dbo].[Customer] AS [t0]
I understood that LINQ selects all columns and then creates the integer array of ID fields.
How can I write a LINQ query which generates this query in SQL Server:
SELECT [t0].[ID] FROM [dbo].[Customer] AS [t0]
Thank you.
© Stack Overflow or respective owner