Linq查询计数和GroupBy

Luí*_*sus 3 c# linq

任何人都可以告诉我如何在Linq中执行此SQL查询?

SELECT [id], COUNT(*) FROM [data].[dbo].[MyTable] GROUP BY [id]
Run Code Online (Sandbox Code Playgroud)

das*_*ght 5

你可以尝试这种方法:

 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
Run Code Online (Sandbox Code Playgroud)