两级组,按LINQ排序

时间:2010-01-26 00:21:20

标签: sql linq sql-order-by group-by

LINQ等效于以下内容:

SELECT Mytable.A, MyTable.B, SUM(MyTable.C)
FROM MyTable
GROUP BY Mytable.A, MyTable.B
ORDER BY Mytable.A, MyTable.B

这在SQL中是微不足道的,但在LINQ中似乎非常困难。我错过了什么?

1 个答案:

答案 0 :(得分:5)

我现在无法访问Visual Studio来测试它,但它应该如下所示:

from record in table
group record by new { record.A, record.B } into recGroup
orderby recGroup.Key.A, recGroup.Key.B
select new { recGroup.Key.A, recGroup.Key.B, C = recGroup.Select(p => p.C).Sum() }