<delect id="sj01t"></delect>
  1. <em id="sj01t"><label id="sj01t"></label></em>
  2. <div id="sj01t"></div>
    1. <em id="sj01t"></em>

            <div id="sj01t"></div>

            c#查詢關鍵字where 子句的運用

            時間:2025-12-03 23:24:15 C語言

            c#查詢關鍵字where 子句的運用

              引導語:where是數據庫中的一個指令,一般用于用于規定選擇的標準。在c#中同樣適用,以下是小編整理的c#查詢關鍵字where 子句的運用,歡迎參考閱讀!

              where 子句用在查詢表達式中,用于指定將在查詢表達式中返回數據源中的哪些元素。它將一個布爾條件(“謂詞”)應用于每個源元素(由范圍變量引用),并返回滿足指定條件的元素。一個查詢表達式可以包含多個 where 子句,一個子句可以包含多個謂詞子表達式。

              示例

              在下面的示例中,where 子句篩選出除小于五的數字外的所有數字。如果移除 where 子句,則會返回數據源中的所有數字。表達式 num < 5 是應用于每個元素的謂詞。

              C#

              class WhereSample

              {

              static void Main()

              {

              /pic/p>

              int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };

              /pic/p>

              var queryLowNums =

              from num in numbers

              where num < 5

              select num;

              /pic/p>

              foreach (var s in queryLowNums)

              {

              Console.Write(s.ToString() + " ");

              }

              }

              }

              /pic/p>

              在單一 where 子句內,可以使用 && 和 || 運算符根據需要指定任意多個謂詞。在下面的示例中,查詢將指定兩個謂詞,以便只選擇小于五的偶數。

              C#

              class WhereSample2

              {

              static void Main()

              {

              /pic/p>

              int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };

              /pic/p>

              var queryLowNums2 =

              from num in numbers

              where num < 5 && num % 2 == 0

              select num;

              /pic/p>

              foreach (var s in queryLowNums2)

              {

              Console.Write(s.ToString() + " ");

              }

              }

              }

              /pic/p>

              where 子句可以包含一個或多個返回布爾值的方法。在下面的示例中,where 子句使用一個方法來確定范圍變量的當前值是偶數還是奇數。

              C# 

              class WhereSample3

              {

              static void Main()

              {

              /pic/p>

              int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };

              /pic/p>

              /pic/p>

              /pic/p>

              var queryEvenNums =

              from num in numbers

              where IsEven(num)

              select num;

              /pic/p>

              foreach (var s in queryEvenNums)

              {

              Console.Write(s.ToString() + " ");

              }

              }

              /pic/p>

              static bool IsEven(int i)

              {

              return i % 2 == 0;

              }

              }

              /pic/p>

              備注

              where 子句是一種篩選機制。除了不能是第一個或最后一個子句外,它幾乎可以放在查詢表達式中的任何位置。where 子句可以出現在 group 子句的前面或后面,具體情況取決于是必須在對源元素進行分組之前還是之后來篩選源元素。

              如果指定的謂詞對于數據源中的元素無效,則會發生編譯時錯誤。這是 LINQ 提供的強類型檢查的一個優點。

              編譯時,where 關鍵字會被轉換為對 Where 標準查詢運算符方法的調用。

            【c#查詢關鍵字where 子句的運用】相關文章:

            c#關鍵字查詢之select 子句運用02-07

            c#查詢關鍵字之join 子句運用方法03-09

            c#查詢關鍵字from 子句的用法11-29

            c#查詢關鍵字之group子句的使用02-24

            c#查詢關鍵字之into的使用02-04

            c#運算符關鍵字is的使用02-03

            C# 術語大全12-24

            Lesson 4:Where is it優秀教案10-23

            c#冒泡排序算法02-03

            <delect id="sj01t"></delect>
            1. <em id="sj01t"><label id="sj01t"></label></em>
            2. <div id="sj01t"></div>
              1. <em id="sj01t"></em>

                      <div id="sj01t"></div>
                      黄色视频在线观看