Compartilhar via


Como criar um grupo aninhado (Guia de Programação em C#)

O exemplo a seguir mostra como criar grupos aninhados em um LINQ expressão de consulta. Cada grupo que é criado de acordo com nível de ano ou o grau do aluno é subdividido em grupos com base nos nomes dos indivíduos.

Exemplo

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        
 */

Observe que três aninhado foreach loops são necessários para iterar sobre os elementos internos de um grupo aninhado.

Compilando o código

Este exemplo contém referências a objetos que são definidos no aplicativo de amostra em Como consultar uma coleção de objetos (Guia de Programação em C#). Para compilar e executar esse método, colá-lo na StudentClass classe nesse aplicativo e adicione uma chamada a partir do Main método.

Quando você adapta este método para seu próprio aplicativo, lembre-se de que o LINQ requer a versão 3.5 da .NET Framework, e o projeto deve conter uma referência a System.Core.dll e o uso de uma diretriz para System. LINQ. LINQ to SQL, LINQ to XML e LINQ to DataSet tipos exigem referências e usos adicionais. Para obter mais informações, consulte Como criar um projeto LINQ.

Consulte também

Conceitos

Expressões de consulta LINQ (Guia de Programação em C#)