방법: 중첩 그룹 만들기(C# 프로그래밍 가이드)

다음 예제에서는 LINQ 쿼리 식에서 중첩 그룹을 만드는 방법을 보여 줍니다. 학년이나 등급에 따라 만들어진 각 그룹은 개인의 이름에 기초한 그룹으로 더 세분화됩니다.

예제

public void QueryNestedGroups()
{
    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;

    // Three nested foreach loops are required to iterate 
    // over all elements of a grouped group. Hover the mouse 
    // cursor over the iteration variables to see their actual type.
    foreach (var outerGroup in queryNestedGroups)
    {
        Console.WriteLine("DataClass.Student Level = {0}", outerGroup.Key);
        foreach (var innerGroup in outerGroup)
        {
            Console.WriteLine("\tNames that begin with: {0}", innerGroup.Key);
            foreach (var innerGroupElement in innerGroup)
            {
                Console.WriteLine("\t\t{0} {1}", innerGroupElement.LastName, innerGroupElement.FirstName);
            }
        }
    }
}
/*
 Output:
DataClass.Student Level = SecondYear
        Names that begin with: Adams
                Adams Terry
        Names that begin with: Garcia
                Garcia Hugo
        Names that begin with: Omelchenko
                Omelchenko Svetlana
DataClass.Student Level = ThirdYear
        Names that begin with: Fakhouri
                Fakhouri Fadi
        Names that begin with: Garcia
                Garcia Debra
        Names that begin with: Tucker
                Tucker Lance
DataClass.Student Level = FirstYear
        Names that begin with: Feng
                Feng Hanying
        Names that begin with: Mortensen
                Mortensen Sven
        Names that begin with: Tucker
                Tucker Michael
DataClass.Student Level = FourthYear
        Names that begin with: Garcia
                Garcia Cesar
        Names that begin with: O'Donnell
                O'Donnell Claire
        Names that begin with: Zabokritski
                Zabokritski Eugene        
 */

중첩 그룹의 내부 요소를 반복하려면 세 개의 중첩 foreach 루프가 필요합니다.

코드 컴파일

이 예제에는 방법: 개체 컬렉션 쿼리(C# 프로그래밍 가이드)에서 샘플 응용 프로그램에 정의된 개체에 대한 참조가 포함되어 있습니다. 이 메서드를 컴파일하고 실행하려면 해당 응용 프로그램의 StudentClass 클래스에 이 메서드를 붙여넣고 이 메서드에 대한 Main 메서드의 호출을 추가합니다.

사용자 고유의 응용 프로그램에 이 메서드를 적용하는 경우 LINQ에서는 .NET Framework 버전 3.5를 필요로 하며 프로젝트에는 System.Core.dll에 대한 참조와 System.Linq에 대한 using 지시문이 포함되어야 합니다. LINQ to SQL, LINQ to XML 및 LINQ to DataSet 형식에는 추가 usings 및 참조가 필요합니다. 자세한 내용은 방법: LINQ 프로젝트 만들기를 참조하십시오.

참고 항목

개념

LINQ 쿼리 식(C# 프로그래밍 가이드)