<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之旅 廣告
                # join 子句(C# 參考) 使用 **join** 子句可以將來自不同源序列并且在對象模型中沒有直接關系的元素相關聯。唯一的要求是每個源中的元素需要共享某個可以進行比較以判斷是否相等的值。例如,食品經銷商可能具有某種產品的供應商列表以及買主列表。例如,可以使用 **join** 子句創建該產品同一指定地區供應商和買主的列表。 **join** 子句接受兩個源序列作為輸入。每個序列中的元素都必須是可以與另一個序列中的相應屬性進行比較的屬性,或者包含一個這樣的屬性。 **join** 子句使用特殊的 **equals** 關鍵字比較指定的鍵是否相等。 **join** 子句執行的所有聯接都是同等聯接。 **join** 子句的輸出形式取決于所執行的聯接的具體類型。以下是三種最常見的聯接類型: * 內部聯接 * 分組聯接 * 左外部聯接 ## 內部聯接 下面的示例演示一個簡單的內部同等聯接。此查詢產生一個“產品名稱/類別”對平面序列。同一類別字符串將出現在多個元素中。如果 categories 中的某個元素不具有匹配的 products,則該類別不會出現在結果中。 ``` var innerJoinQuery = from category in categories join prod in products on category.ID equals prod.CategoryID select new { ProductName = prod.Name, Category = category.Name }; //produces flat sequence ``` 有關更多信息,請參見 [如何:執行內部聯接(C# 編程指南)](https://msdn.microsoft.com/zh-cn/library/bb397941.aspx)。 ## Group Join 含有 **into** 表達式的 **join** 子句稱為分組聯接。 ``` var innerGroupJoinQuery = from category in categories join prod in products on category.ID equals prod.CategoryID into prodGroup select new { CategoryName = category.Name, Products = prodGroup }; ``` 分組聯接會產生一個分層的結果序列,該序列將左側源序列中的元素與右側源序列中的一個或多個匹配元素相關聯。分組聯接沒有等效的關系術語;它本質上是一個對象數組序列。 如果在右側源序列中找不到與左側源中的元素相匹配的元素,則 **join** 子句會為該項產生一個空數組。因此,分組聯接基本上仍然是一種內部同等聯接,區別只在于分組聯接將結果序列組織為多個組。 如果您只選擇分組聯接的結果,則可以訪問各個項,但無法識別結果所匹配的鍵。因此,通常更為有用的做法是選擇分組聯接的結果并放入一個也具有該鍵名的新類型中,如上一個示例所示。 當然,還可以將分組聯接的結果用作其他子查詢的生成器: ``` var innerGroupJoinQuery2 = from category in categories join prod in products on category.ID equals prod.CategoryID into prodGroup from prod2 in prodGroup where prod2.UnitPrice > 2.50M select prod2; ``` 有關更多信息,請參見 [如何:執行分組聯接(C# 編程指南)](https://msdn.microsoft.com/zh-cn/library/bb397905.aspx)。 ## 左外部聯接 在左外部聯接中,將返回左側源序列中的所有元素,即使它們在右側序列中沒有匹配的元素也是如此。若要在 LINQ 中執行左外部聯接,請將 **DefaultIfEmpty** 方法與分組聯接結合起來,以指定要在某個左側元素不具有匹配元素時產生的默認右側元素。可以使用 **null** 作為任何引用類型的默認值,也可以指定用戶定義的默認類型。下面的示例演示了用戶定義的默認類型: ``` var leftOuterJoinQuery = from category in categories join prod in products on category.ID equals prod.CategoryID into prodGroup from item in prodGroup.DefaultIfEmpty(new Product { Name = String.Empty, CategoryID = 0 }) select new { CatName = category.Name, ProdName = item.Name }; ``` 有關更多信息,請參見 [如何:執行左外部聯接(C# 編程指南)](https://msdn.microsoft.com/zh-cn/library/bb397895.aspx)。 ## equals 運算符 **join** 子句執行同等聯接。換句話說,只能基于兩個鍵之間的相等關系進行匹配。其他類型的比較(例如,“greater than”或“not equals”)不受支持。為了表明所有聯接都是同等聯接,**join** 子句使用 **equals** 關鍵字而不是 **==** 運算符。 **equals** 關鍵字只能用在 **join** 子句中,并且它與 **==** 運算符之間存在一個重要區別。對于 **equals**,左鍵使用外部源序列,而右鍵使用內部源序列。外部源僅在 **equals** 的左側位于范圍內,而內部源序列僅在其右側位于范圍內。 ## 非同等聯接 通過使用多個 **from** 子句將新序列單獨引入到查詢中,可以執行非同等聯接、交叉聯接和其他自定義聯接操作。有關更多信息,請參見 [如何:執行自定義聯接操作(C# 編程指南)](https://msdn.microsoft.com/zh-cn/library/bb882533.aspx)。 ## 對象集合聯接與關系表 在 LINQ 查詢表達式中,聯接操作是在對象集合上執行的。不能使用與兩個關系表完全相同的方式“聯接”對象集合。在 LINQ 中,僅當兩個源序列沒有通過任何關系相互聯系時,才需要使用顯式 **join** 子句。使用 LINQ to SQL 時,外鍵表在對象模型中表示為主表的屬性。例如,在 Northwind 數據庫中,Customer 表與 Orders 表之間具有外鍵關系。在將這兩個表映射到對象模型時,Customer 類具有一個 Orders 屬性,該屬性包含與該 Customer 相關聯的 Orders 的集合。實際上,已經為您執行了聯接。 有關在 LINQ to SQL 的上下文中跨相關表執行查詢的更多信息,請參見[如何:映射數據庫關系](https://msdn.microsoft.com/zh-cn/library/bb386950.aspx)。 ## 復合鍵 使用復合鍵可以測試多個值是否相等。有關更多信息,請參見 [如何:使用復合鍵進行聯接(C# 編程指南)](https://msdn.microsoft.com/zh-cn/library/bb907099.aspx)。還可以在 **group** 子句中使用組合鍵。 下面的示例比較了使用相同的匹配鍵對相同數據源執行內部聯接、分組聯接和左外部聯接的結果。這些示例中添加了一些額外的代碼,以便在控制臺顯示中闡明結果。 ``` class JoinDemonstration { #region Data class Product { public string Name { get; set; } public int CategoryID { get; set; } } class Category { public string Name { get; set; } public int ID { get; set; } } // Specify the first data source. List<Category> categories = new List<Category>() { new Category(){Name="Beverages", ID=001}, new Category(){ Name="Condiments", ID=002}, new Category(){ Name="Vegetables", ID=003}, new Category() { Name="Grains", ID=004}, new Category() { Name="Fruit", ID=005} }; // Specify the second data source. List<Product> products = new List<Product>() { new Product{Name="Cola", CategoryID=001}, new Product{Name="Tea", CategoryID=001}, new Product{Name="Mustard", CategoryID=002}, new Product{Name="Pickles", CategoryID=002}, new Product{Name="Carrots", CategoryID=003}, new Product{Name="Bok Choy", CategoryID=003}, new Product{Name="Peaches", CategoryID=005}, new Product{Name="Melons", CategoryID=005}, }; #endregion static void Main(string[] args) { JoinDemonstration app = new JoinDemonstration(); app.InnerJoin(); app.GroupJoin(); app.GroupInnerJoin(); app.GroupJoin3(); app.LeftOuterJoin(); app.LeftOuterJoin2(); // Keep the console window open in debug mode. Console.WriteLine("Press any key to exit."); Console.ReadKey(); } void InnerJoin() { // Create the query that selects // a property from each element. var innerJoinQuery = from category in categories join prod in products on category.ID equals prod.CategoryID select new { Category = category.ID, Product = prod.Name }; Console.WriteLine("InnerJoin:"); // Execute the query. Access results // with a simple foreach statement. foreach (var item in innerJoinQuery) { Console.WriteLine("{0,-10}{1}", item.Product, item.Category); } Console.WriteLine("InnerJoin: {0} items in 1 group.", innerJoinQuery.Count()); Console.WriteLine(System.Environment.NewLine); } void GroupJoin() { // This is a demonstration query to show the output // of a "raw" group join. A more typical group join // is shown in the GroupInnerJoin method. var groupJoinQuery = from category in categories join prod in products on category.ID equals prod.CategoryID into prodGroup select prodGroup; // Store the count of total items (for demonstration only). int totalItems = 0; Console.WriteLine("Simple GroupJoin:"); // A nested foreach statement is required to access group items. foreach (var prodGrouping in groupJoinQuery) { Console.WriteLine("Group:"); foreach (var item in prodGrouping) { totalItems++; Console.WriteLine(" {0,-10}{1}", item.Name, item.CategoryID); } } Console.WriteLine("Unshaped GroupJoin: {0} items in {1} unnamed groups", totalItems, groupJoinQuery.Count()); Console.WriteLine(System.Environment.NewLine); } void GroupInnerJoin() { var groupJoinQuery2 = from category in categories orderby category.ID join prod in products on category.ID equals prod.CategoryID into prodGroup select new { Category = category.Name, Products = from prod2 in prodGroup orderby prod2.Name select prod2 }; //Console.WriteLine("GroupInnerJoin:"); int totalItems = 0; Console.WriteLine("GroupInnerJoin:"); foreach (var productGroup in groupJoinQuery2) { Console.WriteLine(productGroup.Category); foreach (var prodItem in productGroup.Products) { totalItems++; Console.WriteLine(" {0,-10} {1}", prodItem.Name, prodItem.CategoryID); } } Console.WriteLine("GroupInnerJoin: {0} items in {1} named groups", totalItems, groupJoinQuery2.Count()); Console.WriteLine(System.Environment.NewLine); } void GroupJoin3() { var groupJoinQuery3 = from category in categories join product in products on category.ID equals product.CategoryID into prodGroup from prod in prodGroup orderby prod.CategoryID select new { Category = prod.CategoryID, ProductName = prod.Name }; //Console.WriteLine("GroupInnerJoin:"); int totalItems = 0; Console.WriteLine("GroupJoin3:"); foreach (var item in groupJoinQuery3) { totalItems++; Console.WriteLine(" {0}:{1}", item.ProductName, item.Category); } Console.WriteLine("GroupJoin3: {0} items in 1 group", totalItems, groupJoinQuery3.Count()); Console.WriteLine(System.Environment.NewLine); } void LeftOuterJoin() { // Create the query. var leftOuterQuery = from category in categories join prod in products on category.ID equals prod.CategoryID into prodGroup select prodGroup.DefaultIfEmpty(new Product() { Name = "Nothing!", CategoryID = category.ID }); // Store the count of total items (for demonstration only). int totalItems = 0; Console.WriteLine("Left Outer Join:"); // A nested foreach statement is required to access group items foreach (var prodGrouping in leftOuterQuery) { Console.WriteLine("Group:", prodGrouping.Count()); foreach (var item in prodGrouping) { totalItems++; Console.WriteLine(" {0,-10}{1}", item.Name, item.CategoryID); } } Console.WriteLine("LeftOuterJoin: {0} items in {1} groups", totalItems, leftOuterQuery.Count()); Console.WriteLine(System.Environment.NewLine); } void LeftOuterJoin2() { // Create the query. var leftOuterQuery2 = from category in categories join prod in products on category.ID equals prod.CategoryID into prodGroup from item in prodGroup.DefaultIfEmpty() select new { Name = item == null ? "Nothing!" : item.Name, CategoryID = category.ID }; Console.WriteLine("LeftOuterJoin2: {0} items in 1 group", leftOuterQuery2.Count()); // Store the count of total items int totalItems = 0; Console.WriteLine("Left Outer Join 2:"); // Groups have been flattened. foreach (var item in leftOuterQuery2) { totalItems++; Console.WriteLine("{0,-10}{1}", item.Name, item.CategoryID); } Console.WriteLine("LeftOuterJoin2: {0} items in 1 group", totalItems); } } /*Output: InnerJoin: Cola 1 Tea 1 Mustard 2 Pickles 2 Carrots 3 Bok Choy 3 Peaches 5 Melons 5 InnerJoin: 8 items in 1 group. Unshaped GroupJoin: Group: Cola 1 Tea 1 Group: Mustard 2 Pickles 2 Group: Carrots 3 Bok Choy 3 Group: Group: Peaches 5 Melons 5 Unshaped GroupJoin: 8 items in 5 unnamed groups GroupInnerJoin: Beverages Cola 1 Tea 1 Condiments Mustard 2 Pickles 2 Vegetables Bok Choy 3 Carrots 3 Grains Fruit Melons 5 Peaches 5 GroupInnerJoin: 8 items in 5 named groups GroupJoin3: Cola:1 Tea:1 Mustard:2 Pickles:2 Carrots:3 Bok Choy:3 Peaches:5 Melons:5 GroupJoin3: 8 items in 1 group Left Outer Join: Group: Cola 1 Tea 1 Group: Mustard 2 Pickles 2 Group: Carrots 3 Bok Choy 3 Group: Nothing! 4 Group: Peaches 5 Melons 5 LeftOuterJoin: 9 items in 5 groups LeftOuterJoin2: 9 items in 1 group Left Outer Join 2: Cola 1 Tea 1 Mustard 2 Pickles 2 Carrots 3 Bok Choy 3 Nothing! 4 Peaches 5 Melons 5 LeftOuterJoin2: 9 items in 1 group Press any key to exit. */ ``` ## 備注 后面未跟 **into** 的 **join** 子句被轉換為 [Join&lt;TOuter, TInner, TKey, TResult&gt;](https://msdn.microsoft.com/zh-cn/library/bb534675.aspx) 方法調用;后面跟有 **into** 的 **join** 子句被轉換為 [GroupJoin&lt;TOuter, TInner, TKey, TResult&gt;](https://msdn.microsoft.com/zh-cn/library/bb534297.aspx) 方法調用。 ## 請參閱 [查詢關鍵字(C# 參考)](https://msdn.microsoft.com/zh-cn/library/bb310804.aspx) [LINQ 查詢表達式(C# 編程指南)](https://msdn.microsoft.com/zh-cn/library/bb397676.aspx) [Join Operations](https://msdn.microsoft.com/zh-cn/library/bb397908.aspx) [group 子句(C# 參考)](https://msdn.microsoft.com/zh-cn/library/bb384063.aspx) [如何:執行左外部聯接(C# 編程指南)](https://msdn.microsoft.com/zh-cn/library/bb397895.aspx) [如何:執行內部聯接(C# 編程指南)](https://msdn.microsoft.com/zh-cn/library/bb397941.aspx) [如何:執行分組聯接(C# 編程指南)](https://msdn.microsoft.com/zh-cn/library/bb397905.aspx) [如何:對 Join 子句的結果進行排序(C# 編程指南)](https://msdn.microsoft.com/zh-cn/library/bb882517.aspx) [如何:使用復合鍵進行聯接(C# 編程指南)](https://msdn.microsoft.com/zh-cn/library/bb907099.aspx) [如何:安裝示例數據庫](https://msdn.microsoft.com/zh-cn/library/8b6y4c7s.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>

                              哎呀哎呀视频在线观看