Linq Query Count and GroupBy

c#, linq

Solution

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

Problem

Can anyone tell me how to do this SQL Query in Linq? ``` SELECT [id], COUNT(*) FROM [data].[dbo].[MyTable] GROUP BY [id] ```

Original source