我在linq中找到了这个嵌套组的例子。如果我想再添加一个group by
,它会怎样?
var queryNestedGroups =
from student in students
group student by student.Year into newGroup1
from newGroup2 in
(from student in newGroup1
group student by student.LastName)
group newGroup2 by newGroup1.Key;
答案 0 :(得分:1)
首先,让我们重命名示例查询的一些变量:
var queryNestedGroups =
from e in source
group e by e.Key1 into g1
from e1 in
(from e in g1
group e by e.Key2)
group e1 by g1.Key;
现在,您可以使用相同的模式添加另一个嵌套组级别:
var queryNestedGroups =
from e in source
group e by e.Key1 into g1
from e1 in
(from e in g1
group e by e.Key2 into g2
from e2 in
(from e in g2
group e by e.Key3)
group e2 by g2.Key)
group e1 by g1.Key;