任何人都可以告诉我如何在Linq中执行此SQL查询?
SELECT [id], COUNT(*) FROM [data].[dbo].[MyTable] GROUP BY [id]
Run Code Online (Sandbox Code Playgroud)
你可以尝试这种方法:
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)