LINQ Where Clause Filtering Operator

  Total Views : 377
  Comments : 0
 
  Keywords : Linq filtering operator where clause with examples, How to use linq where clause with multiple conditions in c# / vb.net examples, Use linq where clause with query syntax / method syntax

Here we will learn linq where clause / condition filtering operator in c# / vb.net with example, how to use linq where clause / condition with list in query syntax and method syntax with example and how to use linq where clause with multiple conditions in c# / vb.net with examples.

LINQ Filtering Operators

In LINQ Filtering operators are used to filter list / collection data based on filter conditions. In LINQ we have two types of filtering operators those are

 

  1. Where
  2. OfType

Following table shows the more detail regarding filtering operators in LINQ.

 

OperatorDescriptionQuery Syntax
Where This operator is used to select values from list based on predicate functions. where
OfType This operator is used to select values based on their ability to caste a specified type. Not Applicable

LINQ Where Clause Filtering Operator

A filtering operator in LINQ specifies the statement should only affect rows that meet specified criteria. The criteria expressed as predicates, the where clause is not a mandatory clause of LINQ statements, but can be used to limit the number of records affected by a LINQ. The where clause is only used to extract records from select, delete, update and so on. 

LINQ Where Clause Operator in Method Syntax

Following is the syntax of using LINQ where clause filtering operator in method syntax to get data from collection list based on conditions.

 

C# Code

 

IEnumerable<string> result = countries.Where(x => x.StartsWith("A"));

VB.NET Code

 

Dim result As IEnumerable(Of String) = countries.Where(Function(x) x.StartsWith("A"))

Let's see example of using where clause filtering operator in LINQ.

LINQ Where Clause with Method Syntax Example

Following is the example of using LINQ where clause filtering operator with method syntax to filter records based on where condition.

 

C# Code

 

using System;

using System.Collections.Generic;

using System.Linq;

 

namespace Linqtutorials

{

class Program

{

static void Main(string[] args)

{

string[] countries = { "India", "Australia", "USA", "Argentina", "Peru", "China" };

 

IEnumerable<string> result = countries.Where(x => x.StartsWith("A"));

 

foreach (var country in result)

{

Console.WriteLine(country);

}

Console.ReadLine();

}

}

 

}

VB.NET Code

 

Module Module1

 

Sub Main()

Dim countries As String() = {"India", "Australia", "USA", "Argentina", "Peru", "China"}

Dim result As IEnumerable(Of String) = countries.Where(Function(x) x.StartsWith("A"))

For Each country In result

Console.WriteLine(country)

Next

Console.ReadLine()

End Sub

 

End Module

Here we have used filtering operator which is our Where Clause; in program we have an array string which has countries. Now we want to display those countries whose first letter starts with “A”.

 

To do this we have used a where clause to specify a condition just like we do in normal SQL. Then we used a lambda expression to specify an input parameter x and use StartsWith function on the input parameter to denote that we want only those countries from the array whose name starts with “A”.

Output of LINQ Where Clause with Method Syntax Example

We will get output in the form of IEnumerable of type string and iterate through the collection to access the countries. Following is the result of using Where clause in method syntax example.

 

Australia

Argentina

LINQ Where Clause with Query Syntax

We can use filtering operator where clause in query syntax also to filter list / collection data based on conditions. Following is the syntax of defining where clause with query syntax.

 

C# Code

 

IEnumerable<string> result = from x in countries

where x.StartsWith("A")

select x;

VB.NET Code

 

Dim result As IEnumerable(Of String) = From x In countries Where x.StartsWith("A")

If you observe above syntax we are using linq where clause in query syntax to get data from list based on condition.

LINQ Where Clause with Query Syntax Example

Following is the example of using linq where clause in query syntax to get data from list based on conditions.

 

C# Code

 

using System;

using System.Collections.Generic;

using System.Linq;

 

namespace Linqtutorials

{

class Program

{

static void Main(string[] args)

{

string[] countries = { "India", "Australia", "USA", "Argentina", "Peru", "China" };

 

IEnumerable<string> result = from x in countries

where x.StartsWith("A")

select x;

foreach (var country in result)

{

Console.WriteLine(country);

}

Console.ReadLine();

}

}

}

VB.NET Code

 

Module Module1

 

Sub Main()

Dim countries As String() = {"India", "Australia", "USA", "Argentina", "Peru", "China"}

Dim result As IEnumerable(Of String) = From x In countries Where x.StartsWith("A")

For Each country In result

Console.WriteLine(country)

Next

Console.ReadLine()

End Sub

 

End Module

If you observe above code we defined required conditions using LINQ where clause.

Output of LINQ Where Clause in Method Syntax

Following is the result of using LINQ where clause in method syntax to filter data in list based on where condition.

 

Australia

Argentina

LINQ Where Clause with Multiple Conditions

In LINQ we can use Where() clause in query to define multiple conditions like as shown below

 

C# Code

 

IEnumerable<string> result = from x in countries

where x.StartsWith("A")

where x.EndsWith("s")

select x;

VB.NET Code

 

Dim result As IEnumerable(Of String) = From x In countries Where x.StartsWith("A") Where x.EndsWith("a")

This is how we can use linq where clause filtering operator to filter data based on conditions.

 Comments (0)

Be the first to give your valuable feedback

Leave a comment

  • Default Tutlane User