LINQ Filtering Operator-Where Clause

Where clause can be used to specify a particular condition on the data source that we are trying to work on and fetch only the required information.

It is not a mandatory thing to use in LINQ but can be used wherever needed…

result =my_Datasource.Where(“Our Expression”)

LINQ WHERE Function Example using C#

using System;
using System.Collections.Generic;
using System.Linq;

namespace LinqExamplesCSharp
{
class Program
{
static void Main(string[] args)
{
string[] food_Items = { "Chicken 65", "Butter Chicken", "Fish", "Chicken tikka", "Mutton" };
///Trying to select the items with Chicken keyword in it...
IEnumerable<string> result = food_Items.Where(x => x.Contains("Chicken"));
foreach (var item in result)
{
Console.WriteLine(item);
}
Console.ReadLine(); 
}
}
}

Output:
Chicken 65
Butter Chicken
Chicken tikka

LINQ WHERE Function Example using Vb.Net

Module Module1
  Sub Main()
    Dim food_List As String() = {"Chicken 65", "Butter Chicken", "Fish", "Chicken tikka", "Mutton"}
    Dim result As IEnumerable(Of String) = food_List.Where(Function(x) x.Contains("Chicken"))
    For Each item In result
      Console.WriteLine(item)
    Next
    Console.ReadLine()
  End Sub
End Module

Output: Chicken 65
Butter Chicken
Chicken tikka

Previous topic: LINQ Aggregate Functions
Next Topic: LINQ Sorting Operator

For more articles on LINQ, Click here

 

Advertisements

Author: Sharath Raju

Founder@www.ExcelCult.com

One thought

Leave a Reply