我在linq中找到了这个嵌套group by的示例。如果要再添加一个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;
首先,让重命名示例查询的一些变量:
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;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句