<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 如何:執行分組聯接(C# 編程指南) 分組聯接可用于產生分層數據結構。它將第一個集合中的每個元素與第二個集合中的一組相關元素進行配對。 例如,一個名為 Student 的類或關系數據庫表可能包含兩個字段:Id 和 Name。另一個名為 Course 的類或關系數據庫表可能包含兩個字段:StudentId 和 CourseTitle。這兩個數據源的分組聯接(基于匹配的 Student.Id 和 Course.StudentId)會將每個 Student 與一個 Course 對象集合(可能為空)組合在一起。 | ![](https://box.kancloud.cn/2016-01-31_56adb62c1380a.jpg) 注意 | | :-- | | 第一個集合中的每個元素都會出現在分組聯接的結果集內,而無論是否在第二個集合中找到相關元素。如果未找到相關元素,則該元素的相關元素序列為空。因此,結果選擇器可以訪問第一個集合的每個元素。非分組聯接中的結果選擇器與此不同,它無法訪問第一個集合中的那些在第二個集合中沒有匹配元素的元素。 | 本主題中的第一個示例演示如何執行分組聯接;第二個示例演示如何使用分組聯接創建 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](https://msdn.microsoft.com/zh-cn/library/bb387098.aspx)。 ``` 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.Linq](https://msdn.microsoft.com/zh-cn/library/system.linq.aspx) 和 [System.Xml.Linq](https://msdn.microsoft.com/zh-cn/library/system.xml.linq.aspx) 命名空間。 * 從示例中復制代碼,并將其粘貼到 program.cs 文件中的 Main 方法之下。向 Main 方法添加一行代碼,以調用粘入的方法。 * 運行該程序。 ## 請參閱 [Join](https://msdn.microsoft.com/zh-cn/library/system.linq.enumerable.join.aspx) [GroupJoin](https://msdn.microsoft.com/zh-cn/library/system.linq.enumerable.groupjoin.aspx) [Join Operations](https://msdn.microsoft.com/zh-cn/library/bb397908.aspx) [如何:執行內部聯接(C# 編程指南)](https://msdn.microsoft.com/zh-cn/library/bb397941.aspx) [如何:執行左外部聯接(C# 編程指南)](https://msdn.microsoft.com/zh-cn/library/bb397895.aspx) [LINQ to XML](https://msdn.microsoft.com/zh-cn/library/bb387098.aspx) [匿名類型(C# 編程指南)](https://msdn.microsoft.com/zh-cn/library/bb397696.aspx) [匿名類型 (Visual Basic)](https://msdn.microsoft.com/zh-cn/library/bb384767.aspx)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看