LINQ過濾運算符
過濾運算符
過濾是一種操作,以限製結果設定為使得它僅選定元素滿足特定的條件。
運算符 | 描述 | C# 查詢表達式語法 | VB 查詢表達式語法 |
---|---|---|---|
Where | 基於謂詞函數過濾值 | where | Where |
OfType | 基於過濾器值作為一個特定類型 | 不適用 | 不適用 |
查詢表達式 Where - 示例
C#
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(); } } }
VB
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