2016-06-02 4 views
0

linq에서 중첩 그룹의 예제를 발견했습니다. group by을 하나 더 추가하려면 어떻게됩니까?linq에서 3 중첩 된 그룹을 수행하는 방법

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; 

답변

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 into g2 
     from e2 in 
      (from e in g2 
      group e by e.Key3) 
     group e2 by g2.Key) 
    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) 
    group e1 by g1.Key; 

지금 같은 패턴을 사용하여 다른 중첩 된 그룹 수준을 추가 할 수

관련 문제