Nasıl yapılır: İç İçe Geçmiş Grup Oluşturma (C# Programlama Kılavuzu)
Aşağıdaki örnek, iç içe geçmiş grupları oluşturulması gösterilmiştir bir LINQ sorgu ifadesi.Olmak üzere Öğrenci yıl veya notu düzeyine göre oluşturulan her grup daha sonra başka bireylerin adlarına dayalı gruplar halinde ikiye ayrılır.
Örnek
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
*/
Üç iç içe Not foreach döngüleri, iç içe grup iç öğeler üzerinde yineleme için gereklidir.
Kod Derleniyor
Bu örnek örnek uygulamada tanımlı nesnelere başvurular içeren Nasıl yapılır: Nesneler Koleksiyonunu Sorgulama (C# Programlama Kılavuzu).Derlemek ve bu yöntemi çalıştırmak için içine Yapıştır StudentClass bu uygulama sınıfı ve buradan bir çağrı ekleyin Main yöntemi.
Bu yöntem uygulama için uyum LINQ 3.5 sürümü gerektirir unutmayın .NET Framework, proje System.Core.dll ve kullanarak bir başvuru içermelidir System.Linq için yönerge.LINQ sql için XML LINQ ve LINQ DataSet türleri için gerekli ek usings ve başvuru.Daha fazla bilgi için bkz. Nasıl yapılır: Bir LINQ Projesi Oluşturma.