<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之旅 廣告
                # from 子句(C# 參考) 查詢表達式必須以 **from** 子句開頭。另外,查詢表達式還可以包含子查詢,子查詢也是以 **from** 子句開頭。 **from** 子句指定以下內容: * 將對其運行查詢或子查詢的數據源。 * 一個本地范圍變量,表示源序列中的每個元素。 范圍變量和數據源都是強類型。 **from** 子句中引用的數據源的類型必須為 [IEnumerable](https://msdn.microsoft.com/zh-cn/library/system.collections.ienumerable.aspx)、[IEnumerable&lt;T&gt;](https://msdn.microsoft.com/zh-cn/library/9eekhta0.aspx) 或一種派生類型(如 [IQueryable&lt;T&gt;](https://msdn.microsoft.com/zh-cn/library/bb351562.aspx))。 在下面的示例中,numbers 是數據源,而 num 是范圍變量。請注意,這兩個變量都是強類型,即使使用了 [var](https://msdn.microsoft.com/zh-cn/library/bb383973.aspx) 關鍵字也是如此。 ``` class LowNums { static void Main() { // A simple data source. int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; // Create the query. // lowNums is an IEnumerable<int> var lowNums = from num in numbers where num < 5 select num; // Execute the query. foreach (int i in lowNums) { Console.Write(i + " "); } } } // Output: 4 1 3 2 0 ``` ## 范圍變量 如果數據源實現了 [IEnumerable&lt;T&gt;](https://msdn.microsoft.com/zh-cn/library/9eekhta0.aspx),則編譯器可以推斷范圍變量的類型。例如,如果數據源的類型為 **IEnumerable&lt;Customer&gt;**,則推斷出范圍變量的類型為 Customer。僅當數據源是非泛型 **IEnumerable** 類型(如 [ArrayList](https://msdn.microsoft.com/zh-cn/library/system.collections.arraylist.aspx))時,才必須顯式指定數據源類型。有關更多信息,請參見 [How to: Query an ArrayList with LINQ](https://msdn.microsoft.com/zh-cn/library/bb397937.aspx)。 在上一個示例中,num 被推斷為 **int** 類型。由于范圍變量是強類型,因此可以對其調用方法或者在其他操作中使用它。例如,可以不編寫 select num,而編寫 select num.ToString() 使查詢表達式返回一個字符串序列而不是整數序列。或者,也可以編寫 select n + 10 使表達式返回序列 14、11、13、12、10。有關更多信息,請參見 [select 子句(C# 參考)](https://msdn.microsoft.com/zh-cn/library/bb384087.aspx)。 范圍變量類似于 [foreach](https://msdn.microsoft.com/zh-cn/library/ttw7t8t6.aspx) 語句中的迭代變量,只是兩者之間有一個非常重要的區別:范圍變量從不實際存儲來自數據源的數據。范圍變量只是提供了語法上的便利,使查詢能夠描述執行查詢時將發生的事情。有關更多信息,請參見 [Introduction to LINQ Queries (C#)](https://msdn.microsoft.com/zh-cn/library/bb397906.aspx)。 ## 復合 from 子句 在某些情況下,源序列中的每個元素本身可能是序列,也可能包含序列。例如,數據源可能是一個 **IEnumerable&lt;Student&gt;**,其中,序列中的每個 Student 對象都包含一個測驗得分列表。若要訪問每個 Student 元素中的內部列表,可以使用復合 **from** 子句。該技術類似于使用嵌套的 [foreach](https://msdn.microsoft.com/zh-cn/library/ttw7t8t6.aspx) 語句。可以向任一 **from** 子句中添加 [where](https://msdn.microsoft.com/zh-cn/library/6b0scde8.aspx) 或 [orderby](https://msdn.microsoft.com/zh-cn/library/bb383982.aspx) 子句來篩選結果。下面的示例演示了一個 Student 對象序列,其中每個對象都包含一個表示測驗得分的內部整數 List。為了訪問該內部列表,此示例使用了復合 **from** 子句。如有必要,可在兩個 **from** 子句之間再插入子句。 ``` class CompoundFrom { // The element type of the data source. public class Student { public string LastName { get; set; } public List<int> Scores {get; set;} } static void Main() { // Use a collection initializer to create the data source. Note that // each element in the list contains an inner sequence of scores. List<Student> students = new List<Student> { new Student {LastName="Omelchenko", Scores= new List<int> {97, 72, 81, 60}}, new Student {LastName="O'Donnell", Scores= new List<int> {75, 84, 91, 39}}, new Student {LastName="Mortensen", Scores= new List<int> {88, 94, 65, 85}}, new Student {LastName="Garcia", Scores= new List<int> {97, 89, 85, 82}}, new Student {LastName="Beebe", Scores= new List<int> {35, 72, 91, 70}} }; // Use a compound from to access the inner sequence within each element. // Note the similarity to a nested foreach statement. var scoreQuery = from student in students from score in student.Scores where score > 90 select new { Last = student.LastName, score }; // Execute the queries. Console.WriteLine("scoreQuery:"); // Rest the mouse pointer on scoreQuery in the following line to // see its type. The type is IEnumerable<'a>, where 'a is an // anonymous type defined as new {string Last, int score}. That is, // each instance of this anonymous type has two members, a string // (Last) and an int (score). foreach (var student in scoreQuery) { Console.WriteLine("{0} Score: {1}", student.Last, student.score); } // Keep the console window open in debug mode. Console.WriteLine("Press any key to exit."); Console.ReadKey(); } } /* scoreQuery: Omelchenko Score: 97 O'Donnell Score: 91 Mortensen Score: 94 Garcia Score: 97 Beebe Score: 91 */ ``` ## 使用多個 from 子句執行聯接 復合 **from** 子句用于訪問單個數據源中的內部集合。不過,查詢還可以包含多個可從獨立數據源生成補充查詢的 **from** 子句。使用此技術可以執行某些類型的、無法通過使用 [join 子句](https://msdn.microsoft.com/zh-cn/library/bb311040.aspx)執行的聯接操作。 下面的示例演示如何使用兩個 **from** 子句構成兩個數據源的完全交叉聯接。 ``` class CompoundFrom2 { static void Main() { char[] upperCase = { 'A', 'B', 'C' }; char[] lowerCase = { 'x', 'y', 'z' }; // The type of joinQuery1 is IEnumerable<'a>, where 'a // indicates an anonymous type. This anonymous type has two // members, upper and lower, both of type char. var joinQuery1 = from upper in upperCase from lower in lowerCase select new { upper, lower }; // The type of joinQuery2 is IEnumerable<'a>, where 'a // indicates an anonymous type. This anonymous type has two // members, upper and lower, both of type char. var joinQuery2 = from lower in lowerCase where lower != 'x' from upper in upperCase select new { lower, upper }; // Execute the queries. Console.WriteLine("Cross join:"); // Rest the mouse pointer on joinQuery1 to verify its type. foreach (var pair in joinQuery1) { Console.WriteLine("{0} is matched to {1}", pair.upper, pair.lower); } Console.WriteLine("Filtered non-equijoin:"); // Rest the mouse pointer over joinQuery2 to verify its type. foreach (var pair in joinQuery2) { Console.WriteLine("{0} is matched to {1}", pair.lower, pair.upper); } // Keep the console window open in debug mode. Console.WriteLine("Press any key to exit."); Console.ReadKey(); } } /* Output: Cross join: A is matched to x A is matched to y A is matched to z B is matched to x B is matched to y B is matched to z C is matched to x C is matched to y C is matched to z Filtered non-equijoin: y is matched to A y is matched to B y is matched to C z is matched to A z is matched to B z is matched to C */ ``` 有關使用多個 **from** 子句的聯接操作的更多信息,請參見[如何:執行自定義聯接操作(C# 編程指南)](https://msdn.microsoft.com/zh-cn/library/bb882533.aspx)。 ## 請參閱 [查詢關鍵字(C# 參考)](https://msdn.microsoft.com/zh-cn/library/bb310804.aspx) [LINQ 查詢表達式(C# 編程指南)](https://msdn.microsoft.com/zh-cn/library/bb397676.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>

                              哎呀哎呀视频在线观看