Reputation: 1678
Can anyone tell me how to do this SQL Query in Linq?
SELECT [id], COUNT(*) FROM [data].[dbo].[MyTable] GROUP BY [id]
Upvotes: 3
Views: 1287
Reputation: 12682
you can try this
var res = from r in MyTable
group p by r.id into grouped
select new {id = g.key, total = g.Count()};
then, when you have to use it, just do ToList()
Also you can do it after the select new
.
I don't have Visual Studio 2010
here to try it, but
I think it will work
Upvotes: 2
Reputation: 727047
You can try this approach:
var res = ctx.MyTable // Start with your table
.GroupBy(r => r.id) / Group by the key of your choice
.Select( g => new {Id = g.Key, Count = g.Count()}) // Create an anonymous type w/results
.ToList(); // Convert the results to List
Upvotes: 5