3

谁能告诉我如何在 Linq 中执行此 SQL 查询?

SELECT [id], COUNT(*) FROM [data].[dbo].[MyTable] GROUP BY [id]
4

2 回答 2

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
于 2012-07-02T01:19:03.590 回答
2

你可以试试这个

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这里尝试,但我认为它会起作用

于 2012-07-02T02:00:15.337 回答