谁能告诉我如何在 Linq 中执行此 SQL 查询?
SELECT [id], COUNT(*) FROM [data].[dbo].[MyTable] GROUP BY [id]
您可以尝试这种方法:
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
你可以试试这个
var res = from r in MyTable
group p by r.id into grouped
select new {id = g.key, total = g.Count()};
然后,当你必须使用它时,只需ToList()
在select new
. 我不必在Visual Studio 2010
这里尝试,但我认为它会起作用