Cómo: Crear grupos anidados (Guía de programación de C#)

En el ejemplo siguiente se muestra cómo crear grupos anidados en una expresión de consulta LINQ. Cada grupo que se crea según el curso y el nivel académico se subdivide en grupos según los nombres de las personas.

Ejemplo

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 se requieren tres bucles foreach anidados para procesar una iteración en los elementos internos de un grupo anidado.

Compilar el código

Este ejemplo contiene referencias a objetos definidos en la aplicación de ejemplo del tema Cómo: Realizar una consulta en una colección de objetos (Guía de programación de C#). Para compilar y ejecutar este método, péguelo en la clase StudentClass de esa aplicación y agregue una llamada a éste desde el método Main.

Cuando adapte este método a su propia aplicación, recuerde que LINQ requiere la versión 3.5 de .NET Framework y que el proyecto debe contener una referencia a System.Core.dll y una directiva using para System.Linq. Los tipos LINQ to SQL, LINQ to XML y LINQ to DataSet requieren directivas using y referencias adicionales. Para obtener más información, vea Cómo: Crear un proyecto con LINQ.

Vea también

Conceptos

Expresiones de consultas LINQ (Guía de programación de C#)