2
誰能告訴我如何在Linq中執行此SQL查詢?Linq查詢計數和GroupBy
SELECT [id], COUNT(*) FROM [data].[dbo].[MyTable] GROUP BY [id]
誰能告訴我如何在Linq中執行此SQL查詢?Linq查詢計數和GroupBy
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
這裏試試,但是 我認爲它會工作
謝謝。我如何通過Count命令? –
@pirezas在'Select'和'ToList'之間添加'.OrderBy(v => v.Count)'。 – dasblinkenlight