<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>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # 如何:執行自定義聯接操作(C# 編程指南) 此示例演示如何執行無法使用 **join** 子句執行的聯接操作。在查詢表達式中,**join** 子句僅適用于同等聯接(這是迄今為止最常見的聯接操作類型),并針對同等聯接進行了優化。執行同等聯接時,一般總是可以通過使用 **join** 子句獲得最佳性能。 但是,在下面一些情況中,無法使用 **join** 子句: * 聯接是在不等式(非同等聯接)上斷言的。 * 聯接是在多個等式或不等式上斷言的。 * 必須在聯接操作之前為右側(內部)序列引入一個臨時范圍變量。 若要執行非同等聯接,可以使用多個 **from** 子句單獨引入每個數據源。然后,在 **where** 子句中將謂詞表達式應用于每個源的范圍變量。該表達式還可以采用方法調用的形式。 | ![](https://box.kancloud.cn/2016-01-31_56adb62c1380a.jpg) 注意 | | :-- | | 不要將這種自定義聯接操作與使用多個 **from** 子句訪問內部集合相混淆。有關更多信息,請參見 [join 子句(C# 參考)](https://msdn.microsoft.com/zh-cn/library/bb311040.aspx)。 | 下面示例中的第一個方法演示了一個簡單的交叉聯接。必須慎用交叉聯接,因為它們可能產生非常大的結果集。但在某些方案中,可以使用它們創建源序列以供運行附加查詢。 第二個方法產生其類別 ID 列在左側類別列表中的所有產品的序列。請注意,這種方法使用 **let** 子句和 **Contains** 方法創建了一個臨時數組。還可以在查詢前創建該數組并去掉第一個 **from** 子句。 ``` class CustomJoins { #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}, }; // Specify the second data source. List<Product> products = new List<Product>() { 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}, new Product{Name="Ice Cream", CategoryID=007}, new Product{Name="Mackerel", CategoryID=012}, }; #endregion static void Main() { CustomJoins app = new CustomJoins(); app.CrossJoin(); app.NonEquijoin(); Console.WriteLine("Press any key to exit."); Console.ReadKey(); } void CrossJoin() { var crossJoinQuery = from c in categories from p in products select new { c.ID, p.Name }; Console.WriteLine("Cross Join Query:"); foreach (var v in crossJoinQuery) { Console.WriteLine("{0,-5}{1}", v.ID, v.Name); } } void NonEquijoin() { var nonEquijoinQuery = from p in products let catIds = from c in categories select c.ID where catIds.Contains(p.CategoryID) == true select new { Product = p.Name, CategoryID = p.CategoryID }; Console.WriteLine("Non-equijoin query:"); foreach (var v in nonEquijoinQuery) { Console.WriteLine("{0,-5}{1}", v.CategoryID, v.Product); } } } /* Output: Cross Join Query: 1 Tea 1 Mustard 1 Pickles 1 Carrots 1 Bok Choy 1 Peaches 1 Melons 1 Ice Cream 1 Mackerel 2 Tea 2 Mustard 2 Pickles 2 Carrots 2 Bok Choy 2 Peaches 2 Melons 2 Ice Cream 2 Mackerel 3 Tea 3 Mustard 3 Pickles 3 Carrots 3 Bok Choy 3 Peaches 3 Melons 3 Ice Cream 3 Mackerel Non-equijoin query: 1 Tea 2 Mustard 2 Pickles 3 Carrots 3 Bok Choy Press any key to exit. */ ``` 在下面的示例中,查詢必須基于匹配鍵聯接兩個序列,而對于內部(右側)序列而言,無法在 join 子句本身之前獲取這些鍵。如果此聯接是使用 **join** 子句執行的,則必須為每個元素調用 **Split** 方法。使用多個 **from** 子句可使查詢避免反復進行方法調用的系統開銷。然而,由于 **join** 進行了優化,因此在此特定情況下,它仍然可能比使用多個 **from** 子句快。結果會有所不同,主要取決于方法調用的系統開銷有多大。 ``` class MergeTwoCSVFiles { static void Main() { // See section Compiling the Code for information about the data files. string[] names = System.IO.File.ReadAllLines(@"../../../names.csv"); string[] scores = System.IO.File.ReadAllLines(@"../../../scores.csv"); // Merge the data sources using a named type. // You could use var instead of an explicit type for the query. IEnumerable<Student> queryNamesScores = // Split each line in the data files into an array of strings. from name in names let x = name.Split(',') from score in scores let s = score.Split(',') // Look for matching IDs from the two data files. where x[2] == s[0] // If the IDs match, build a Student object. select new Student() { FirstName = x[0], LastName = x[1], ID = Convert.ToInt32(x[2]), ExamScores = (from scoreAsText in s.Skip(1) select Convert.ToInt32(scoreAsText)). ToList() }; // Optional. Store the newly created student objects in memory // for faster access in future queries List<Student> students = queryNamesScores.ToList(); foreach (var student in students) { Console.WriteLine("The average score of {0} {1} is {2}.", student.FirstName, student.LastName, student.ExamScores.Average()); } //Keep console window open in debug mode Console.WriteLine("Press any key to exit."); Console.ReadKey(); } } class Student { public string FirstName { get; set; } public string LastName { get; set; } public int ID { get; set; } public List<int> ExamScores { get; set; } } /* Output: The average score of Omelchenko Svetlana is 82.5. The average score of O'Donnell Claire is 72.25. The average score of Mortensen Sven is 84.5. The average score of Garcia Cesar is 88.25. The average score of Garcia Debra is 67. The average score of Fakhouri Fadi is 92.25. The average score of Feng Hanying is 88. The average score of Garcia Hugo is 85.75. The average score of Tucker Lance is 81.75. The average score of Adams Terry is 85.25. The average score of Zabokritski Eugene is 83. The average score of Tucker Michael is 92. */ ``` ## 編譯代碼 * 創建一個面向 .NET Framework 3.5 或更高版本的 Visual Studio 控制臺應用程序項目。默認情況下,該項目具有一個對 System.Core.dll 的引用以及一條針對 System.Linq 命名空間的 **using** 指令。 * 將 Program 類替換為上述示例中的代碼。 * 按照 [How to: Join Content from Dissimilar Files (LINQ)](https://msdn.microsoft.com/zh-cn/library/bb882647.aspx) 中的指令設置數據文件(scores.csv 和 names.csv)。 * 按 F5 編譯并運行程序。 * 按任意鍵退出控制臺窗口。 ## 請參閱 [LINQ 查詢表達式(C# 編程指南)](https://msdn.microsoft.com/zh-cn/library/bb397676.aspx) [join 子句(C# 參考)](https://msdn.microsoft.com/zh-cn/library/bb311040.aspx) [Join Operations](https://msdn.microsoft.com/zh-cn/library/bb397908.aspx) [如何:對 Join 子句的結果進行排序(C# 編程指南)](https://msdn.microsoft.com/zh-cn/library/bb882517.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>

                              哎呀哎呀视频在线观看