开发者

Two level group by, order by using LINQ

开发者 https://www.devze.com 2022-12-18 06:17 出处:网络
What is the LINQ equivalent of the following: SELECT Mytable.A, MyTable.B, SUM(MyTable.C) FROM MyTable GROUP BY Mytable.A, MyTable.B

What is the LINQ equivalent of the following:

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

This is trivi开发者_开发知识库al in SQL, but seems to be very difficult in LINQ. What am I missing?


I don't have access to Visual Studio right now to test it out but it should look like this:

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() }
0

精彩评论

暂无评论...
验证码 换一张
取 消