過濾是一種操作,以限制結果設定為使得它僅選定元素滿足特定的條件。
運算子 | 描述 | C# 查詢表示式語法 | VB 查詢表示式語法 |
---|---|---|---|
Where | 基於謂詞函式過濾值 | where | Where |
OfType | 基於過濾器值作為一個特定型別 | 不適用 | 不適用 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Operators { class Program { static void Main(string[] args) { string[] words = { "humpty", "dumpty", "set", "on", "a", "wall" }; IEnumerable<string> query = from word in words where word.Length == 3 select word; foreach (string str in query) Console.WriteLine(str); Console.ReadLine(); } } }
Module Module1 Sub Main() Dim words As String() = {"humpty", "dumpty", "set", "on", "a", "wall"} Dim query = From word In words Where word.Length = 3 Select word For Each n In query Console.WriteLine(n) Next Console.ReadLine() End Sub End Module
當在C#或VB上面的程式碼被編譯和執行時,它產生了以下結果:
set