共用方式為


HOW TO:執行群組聯結 (C# 程式設計手冊)

更新:2007 年 11 月

群組聯結對產生階層式資料結構相當有用。它會將第一個集合的每個項目與第二個集合的相關項目組進行配對。

例如,名為 Student 的類別或關聯式資料庫資料表可能包含兩個欄位:[ID] 和 [名稱]。名為 Course 的第二個類別或關聯式資料庫資料表可能包含兩個欄位:[StudentId] 和 [CourseTitle]。這兩個資料來源的群組聯結會根據相符的 Student.Id 和 Course.StudentId,將每個 Student 與 Course 物件集合 (可能是空的) 群組在一起。

注意事項:

無論是否在第二個集合中找到相關項目,第一個集合的每個項目都會顯示在群組聯結的結果集中。在沒有找到相關項目的情況下,該項目的相關項目序列是空的。結果選取器因此可以存取第一個集合的每個項目。這與無群組聯結的結果選取器不同,如果在第二個集合中沒有相符項目,該選取器就無法存取第一個集合中的項目。

本主題中的第一個範例顯示如何執行群組聯結。第二個範例顯示如何使用群組聯結建立 XML 項目。

範例

群組聯結範例

下列範例根據符合 Pet.Owner 屬性的 Person,執行型別 Person 和 Pet 之物件的群組聯結。不像無群組聯結會產生每個相符項目的配對,群組聯結只會針對第一個集合的每個項目產生一個結果物件,在此範例中為 Person 物件。第二個集合的對應項目 (在此範例中為 Pet 物件) 會群組至集合。最後,結果選取器函式會針對由 Person.FirstName 和 Pet 物件集合組成的每個相符項目建立匿名型別。

class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

class Pet
{
    public string Name { get; set; }
    public Person Owner { get; set; }
}

/// <summary>
/// This example performs a grouped join.
/// </summary>
public static void GroupJoinExample()
{
    Person magnus = new Person { FirstName = "Magnus", LastName = "Hedlund" };
    Person terry = new Person { FirstName = "Terry", LastName = "Adams" };
    Person charlotte = new Person { FirstName = "Charlotte", LastName = "Weiss" };
    Person arlene = new Person { FirstName = "Arlene", LastName = "Huff" };

    Pet barley = new Pet { Name = "Barley", Owner = terry };
    Pet boots = new Pet { Name = "Boots", Owner = terry };
    Pet whiskers = new Pet { Name = "Whiskers", Owner = charlotte };
    Pet bluemoon = new Pet { Name = "Blue Moon", Owner = terry };
    Pet daisy = new Pet { Name = "Daisy", Owner = magnus };

    // Create two lists.
    List<Person> people = new List<Person> { magnus, terry, charlotte, arlene };
    List<Pet> pets = new List<Pet> { barley, boots, whiskers, bluemoon, daisy };

    // Create a list where each element is an anonymous type
    // that contains the person's first name and a collection of 
    // pets that are owned by them.
    var query = from person in people
                join pet in pets on person equals pet.Owner into gj
                select new { OwnerName = person.FirstName, Pets = gj };

    foreach (var v in query)
    {
        // Output the owner's name.
        Console.WriteLine("{0}:", v.OwnerName);
        // Output each of the owner's pet's names.
        foreach (Pet pet in v.Pets)
            Console.WriteLine("  {0}", pet.Name);
    }
}

// This code produces the following output:
//
// Magnus:
//   Daisy
// Terry:
//   Barley
//   Boots
//   Blue Moon
// Charlotte:
//   Whiskers
// Arlene:

建立 XML 範例的群組聯結

群組聯結適合用於使用 LINQ to XML 建立 XML。下列範例類似於前述範例,除了結果選取器函式不是建立匿名型別,而是建立代表聯結物件的 XML 項目。如需 LINQ to XML 的詳細資訊,請參閱 LINQ to XML

class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

class Pet
{
    public string Name { get; set; }
    public Person Owner { get; set; }
}

/// <summary>
/// This example creates XML output from a grouped join.
/// </summary>
public static void GroupJoinXMLExample()
{
    Person magnus = new Person { FirstName = "Magnus", LastName = "Hedlund" };
    Person terry = new Person { FirstName = "Terry", LastName = "Adams" };
    Person charlotte = new Person { FirstName = "Charlotte", LastName = "Weiss" };
    Person arlene = new Person { FirstName = "Arlene", LastName = "Huff" };

    Pet barley = new Pet { Name = "Barley", Owner = terry };
    Pet boots = new Pet { Name = "Boots", Owner = terry };
    Pet whiskers = new Pet { Name = "Whiskers", Owner = charlotte };
    Pet bluemoon = new Pet { Name = "Blue Moon", Owner = terry };
    Pet daisy = new Pet { Name = "Daisy", Owner = magnus };

    // Create two lists.
    List<Person> people = new List<Person> { magnus, terry, charlotte, arlene };
    List<Pet> pets = new List<Pet> { barley, boots, whiskers, bluemoon, daisy };

    // Create XML to display the hierarchical organization of people and their pets.
    XElement ownersAndPets = new XElement("PetOwners",
        from person in people
        join pet in pets on person equals pet.Owner into gj
        select new XElement("Person",
            new XAttribute("FirstName", person.FirstName),
            new XAttribute("LastName", person.LastName),
            from subpet in gj
            select new XElement("Pet", subpet.Name)));

    Console.WriteLine(ownersAndPets);
}

// This code produces the following output:
//
// <PetOwners>
//   <Person FirstName="Magnus" LastName="Hedlund">
//     <Pet>Daisy</Pet>
//   </Person>
//   <Person FirstName="Terry" LastName="Adams">
//     <Pet>Barley</Pet>
//     <Pet>Boots</Pet>
//     <Pet>Blue Moon</Pet>
//   </Person>
//   <Person FirstName="Charlotte" LastName="Weiss">
//     <Pet>Whiskers</Pet>
//   </Person>
//   <Person FirstName="Arlene" LastName="Huff" />
// </PetOwners>

編譯程式碼

  • 在 Visual Studio 中建立 [主控台應用程式] 專案。

  • 如果未參考 System.Core.dll 和 System.Xml.Linq.dll,請加入參考。

  • 包含 System.LinqSystem.Xml.Linq 命名空間。

  • 從範例複製程式碼,並將其貼入 program.cs 檔中 Main 方法的下方。將一行程式碼加入至 Main 方法,以呼叫您所貼上的方法。

  • 執行程式。

請參閱

工作

HOW TO:執行內部聯結 (C# 程式設計手冊)

HOW TO:執行左外部聯結 (C# 程式設計手冊)

概念

聯結作業

匿名型別

參考

Join

GroupJoin

匿名型別 (C# 程式設計手冊)

其他資源

LINQ to XML