Filtering Operators in LINQ
Filtering is an operation to restrict the result set such that it has only selected elements satisfying a particular condition.
Operator | Description | C# Query Expression Syntax | VB Query Expression Syntax |
---|---|---|---|
Where | Filter values based on a predicate function | where | Where |
OfType | Filter values based on their ability to be as a specified type | Not Applicable | Not Applicable |
Example of Where - Query Expression
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
When the above code in C# or VB is compiled and executed, it produces the following result −
set
linq_query_operators.htm
Advertisements