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

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # where 子句(C# 參考) **where** 子句用在查詢表達式中,用于指定將在查詢表達式中返回數據源中的哪些元素。它將一個布爾條件(“謂詞”)應用于每個源元素(由范圍變量引用),并返回滿足指定條件的元素。一個查詢表達式可以包含多個 **where** 子句,一個子句可以包含多個謂詞子表達式。 在下面的示例中,**where** 子句篩選出除小于五的數字外的所有數字。如果移除 **where** 子句,則會返回數據源中的所有數字。表達式 num &lt; 5 是應用于每個元素的謂詞。 ``` class WhereSample { static void Main() { // Simple data source. Arrays support IEnumerable<T>. int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; // Simple query with one predicate in where clause. var queryLowNums = from num in numbers where num < 5 select num; // Execute the query. foreach (var s in queryLowNums) { Console.Write(s.ToString() + " "); } } } //Output: 4 1 3 2 0 ``` 在單一 **where** 子句內,可以使用 [&&](https://msdn.microsoft.com/zh-cn/library/2a723cdk.aspx) 和 [||](https://msdn.microsoft.com/zh-cn/library/6373h346.aspx) 運算符根據需要指定任意多個謂詞。在下面的示例中,查詢將指定兩個謂詞,以便只選擇小于五的偶數。 ``` class WhereSample2 { static void Main() { // Data source. int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; // Create the query with two predicates in where clause. var queryLowNums2 = from num in numbers where num < 5 && num % 2 == 0 select num; // Execute the query foreach (var s in queryLowNums2) { Console.Write(s.ToString() + " "); } Console.WriteLine(); // Create the query with two where clause. var queryLowNums3 = from num in numbers where num < 5 where num % 2 == 0 select num; // Execute the query foreach (var s in queryLowNums3) { Console.Write(s.ToString() + " "); } } } // Output: // 4 2 0 // 4 2 0 ``` **where** 子句可以包含一個或多個返回布爾值的方法。在下面的示例中,**where** 子句使用一個方法來確定范圍變量的當前值是偶數還是奇數。 ``` class WhereSample3 { static void Main() { // Data source int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; // Create the query with a method call in the where clause. // Note: This won't work in LINQ to SQL unless you have a // stored procedure that is mapped to a method by this name. var queryEvenNums = from num in numbers where IsEven(num) select num; // Execute the query. foreach (var s in queryEvenNums) { Console.Write(s.ToString() + " "); } } // Method may be instance method or static method. static bool IsEven(int i) { return i % 2 == 0; } } //Output: 4 8 6 2 0 ``` ## 備注 **where** 子句是一種篩選機制。除了不能是第一個或最后一個子句外,它幾乎可以放在查詢表達式中的任何位置。 **where** 子句可以出現在 [group](https://msdn.microsoft.com/zh-cn/library/bb384063.aspx) 子句的前面或后面,具體情況取決于是必須在對源元素進行分組之前還是之后來篩選源元素。 如果指定的謂詞對于數據源中的元素無效,則會發生編譯時錯誤。這是 LINQ 提供的強類型檢查的一個優點。 編譯時,**where** 關鍵字會被轉換為對 [Where](https://msdn.microsoft.com/zh-cn/library/system.linq.enumerable.where.aspx) 標準查詢運算符方法的調用。 ## 請參閱 [查詢關鍵字(C# 參考)](https://msdn.microsoft.com/zh-cn/library/bb310804.aspx) [from 子句(C# 參考)](https://msdn.microsoft.com/zh-cn/library/bb383978.aspx) [select 子句(C# 參考)](https://msdn.microsoft.com/zh-cn/library/bb384087.aspx) [Filtering Data](https://msdn.microsoft.com/zh-cn/library/bb546161.aspx) [LINQ 查詢表達式(C# 編程指南)](https://msdn.microsoft.com/zh-cn/library/bb397676.aspx) [Getting Started with LINQ in C#](https://msdn.microsoft.com/zh-cn/library/bb397933.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>

                              哎呀哎呀视频在线观看