;

Filtering Operator - Where


In this tutorial, we will learn about filtering operator - Where in LINQ

Filtering Operator - Where

In LINQ, Filtering operators are used to filter the data from the collection based on the given criteria. In this tutorial, we will learn about the filtering operator - Where.

Where

The Where operator is used to filter the collection based on the specified conditions and returns the new filtered collection. You can provide specific a condition where the operator uses lambda expression or by using Func delegate type. And the specific criteria expressed as predicates.

The Where operator has the following two overload methods and both the overload methods accept a Func delegate type parameter where One overload requires an Func<TSource, bool> input parameter and the second overload method requires an Func<TSource, int, bool> input parameter, where int is for index. These overload methods are as follows:

Overload Method
public static System.Collections.Generic.IEnumerable<TSource> Where<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,bool> predicate);

public static System.Collections.Generic.IEnumerable<TSource> Where<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,int,bool> predicate);

Example of Where clause in Query Syntax

Example 1: Where Clause - Query Syntax 

In this example, we find the employee whose salary is between 150000 and 250000 using query syntax.

Example 1: Where Clause - Query Syntax 
using System;
using System.Linq;
using System.Collections.Generic;

					
public class Program
{
	public static void Main()
	{
		// Employee collection
		IList<Employee> employeelist = new List<Employee>() { 
				new Employee() { EmpID = 1, EmpName = "John", City = "New York", Salary = 130000} ,
				new Employee() { EmpID = 2, EmpName = "Moin", City = "New Orleans", Salary = 210000 } ,
				new Employee() { EmpID = 3, EmpName = "Bill", City = "Seattle", Salary = 180000 } ,
				new Employee() { EmpID = 4, EmpName = "Ram" , City = "Delhi", Salary = 200000} ,
				new Employee() { EmpID = 5, EmpName = "Ron" , City = "San Jose", Salary = 150000 } 
			};
		
		// LINQ Query Syntax to find out Employee 
		// Whose Salary in Between 150000 and 250000
		var filteredEmployee = from s in employeelist
							  where s.Salary > 150000 && s.Salary < 250000
							  select s;
		Console.WriteLine("Employee Whose Salary in Between 150000 and 250000:");
						  
		foreach(Employee emp in filteredEmployee){			
			Console.WriteLine(emp.EmpName);
		}
	}
}

public class Employee{

	public int EmpID { get; set; }
	public string EmpName { get; set; }
	public string City { get; set; }
	public int Salary { get; set; }
	
}
Output
Employee Whose Salary in Between 150000 and 250000:
Moin
Bill
Ram

Example 2: Where Clause - Query Syntax

In this example, we find the employee whose salary is between 150000 and 250000 using query syntax. To find the employee whose salary is between 150000 and 250000, we used the second overloaded method as you can see in the below example:

Example 2: Where Clause - Query Syntax
using System;
using System.Linq;
using System.Collections.Generic;


public class Program
{
    public static void Main()
    {
        // Employee collection
        IList<Employee> employeelist = new List<Employee>() {
                new Employee() { EmpID = 1, EmpName = "John", City = "New York", Salary = 130000} ,
                new Employee() { EmpID = 2, EmpName = "Moin", City = "New Orleans", Salary = 210000 } ,
                new Employee() { EmpID = 3, EmpName = "Bill", City = "Seattle", Salary = 180000 } ,
                new Employee() { EmpID = 4, EmpName = "Ram" , City = "Delhi", Salary = 200000} ,
                new Employee() { EmpID = 5, EmpName = "Ron" , City = "San Jose", Salary = 150000 }
            };

        // LINQ Query Syntax to find out Employee 
        // Whose Salary in Between 150000 and 250000
        var filteredEmployee = (from data in employeelist.Select((Data, index) => new { employee = Data, Index = index })
                                where data.employee.Salary > 150000 && data.employee.Salary < 250000
                                select new
                                {
                                    EmployeeName = data.employee.EmpName,
                                    Salary = data.employee.Salary,
                                    IndexPosition = data.Index
                                });
        Console.WriteLine("Employee Whose Salary in Between 150000 and 250000:");

        foreach (var emp in filteredEmployee)
        {
            Console.WriteLine($"Index : {emp.IndexPosition}; Name : {emp.EmployeeName}, Salary : {emp.Salary}");
        }

        Console.ReadKey();
    }
}

public class Employee
{

    public int EmpID { get; set; }
    public string EmpName { get; set; }
    public string City { get; set; }
    public int Salary { get; set; }

}
Output
Employee Whose Salary in Between 150000 and 250000:
Index : 1; Name : Moin, Salary : 210000
Index : 2; Name : Bill, Salary : 180000
Index : 3; Name : Ram, Salary : 200000

Example of Where clause in Method Syntax

Example 1: Where Clause - Method Syntax

In this example, we find the employee whose salary is between 150000 and 250000 using method syntax.

Example 1: Where Clause - Method Syntax
using System;
using System.Linq;
using System.Collections.Generic;


public class Program
{
    public static void Main()
    {
        // Employee collection
        IList<Employee> employeelist = new List<Employee>() {
                new Employee() { EmpID = 1, EmpName = "John", City = "New York", Salary = 130000} ,
                new Employee() { EmpID = 2, EmpName = "Moin", City = "New Orleans", Salary = 210000 } ,
                new Employee() { EmpID = 3, EmpName = "Bill", City = "Seattle", Salary = 180000 } ,
                new Employee() { EmpID = 4, EmpName = "Ram" , City = "Delhi", Salary = 200000} ,
                new Employee() { EmpID = 5, EmpName = "Ron" , City = "San Jose", Salary = 150000 }
            };

        // LINQ Method Syntax to find out Employee 
        // Whose Salary in Between 150000 and 250000
        var filteredEmployee = employeelist
                               .Where(s => s.Salary > 150000 && s.Salary < 250000)
                               .Select(x => x);
                               

        Console.WriteLine("Employee Whose Salary in Between 150000 and 250000:");

        foreach (Employee emp in filteredEmployee)
        {
            Console.WriteLine(emp.EmpName);
        }

        Console.ReadKey();
    }
}

public class Employee
{

    public int EmpID { get; set; }
    public string EmpName { get; set; }
    public string City { get; set; }
    public int Salary { get; set; }

}
Output
Employee Whose Salary in Between 150000 and 250000:
Moin
Bill
Ram

Example 2: Where Clause - Method Syntax

In this example, we find the employee whose salary is between 150000 and 250000 using method syntax. To find the employee whose salary is between 150000 and 250000, we used the second overloaded method as you can see in the below example:

Example 2: Where Clause - Method Syntax
using System;
using System.Linq;
using System.Collections.Generic;


public class Program
{
    public static void Main()
    {
        IList<Employee> employeelist = new List<Employee>() {
                new Employee() { EmpID = 1, EmpName = "John", City = "New York", Salary = 130000} ,
                new Employee() { EmpID = 2, EmpName = "Moin", City = "New Orleans", Salary = 210000 } ,
                new Employee() { EmpID = 3, EmpName = "Bill", City = "Seattle", Salary = 180000 } ,
                new Employee() { EmpID = 4, EmpName = "Ram" , City = "Delhi", Salary = 200000} ,
                new Employee() { EmpID = 5, EmpName = "Ron" , City = "San Jose", Salary = 150000 }
            };

        // LINQ Method Syntax to find out Employee 
        // Whose Salary in Between 150000 and 250000
        var MethodSyntax = employeelist.Select((Data, index) => new { employee = Data, Index = index })
                           .Where(emp => emp.employee.Salary > 150000 && emp.employee.Salary < 250000)
                           .Select(emp => new
                           {
                               EmployeeName = emp.employee.EmpName,
                               Salary = emp.employee.Salary,
                               IndexPosition = emp.Index
                           });

        Console.WriteLine("Employee Whose Salary in Between 150000 and 250000:");

        foreach (var emp in MethodSyntax)
        {
            Console.WriteLine($"Index : {emp.IndexPosition}; Name : {emp.EmployeeName}, Salary : {emp.Salary}");
        }

        Console.ReadKey();
    }
}

public class Employee
{

    public int EmpID { get; set; }
    public string EmpName { get; set; }
    public string City { get; set; }
    public int Salary { get; set; }

}
Output
Employee Whose Salary in Between 150000 and 250000:
Index : 1; Name : Moin, Salary : 210000
Index : 2; Name : Bill, Salary : 180000
Index : 3; Name : Ram, Salary : 200000

Multiple Where clause

In LINQ, you can call the multiple Where() extension method in a single LINQ query. Below are the examples of both query syntax and method syntax:

Example 1: Multiple Where Clause using Query Syntax 
Example 1: Multiple Where Clause using Query Syntax 
using System;
using System.Linq;
using System.Collections.Generic;


public class Program
{
    public static void Main()
    {
        IList<Employee> employeelist = new List<Employee>() {
                new Employee() { EmpID = 1, EmpName = "John", City = "New York", Salary = 130000} ,
                new Employee() { EmpID = 2, EmpName = "Moin", City = "New Orleans", Salary = 210000 } ,
                new Employee() { EmpID = 3, EmpName = "Bill", City = "Seattle", Salary = 180000 } ,
                new Employee() { EmpID = 4, EmpName = "Ram" , City = "Delhi", Salary = 200000} ,
                new Employee() { EmpID = 5, EmpName = "Ron" , City = "San Jose", Salary = 150000 }
            };

        // LINQ Query Syntax to find out Employee 
        // Whose Salary in Between 150000 and 250000
        var QuerySyntax = from s in employeelist
                          where s.Salary > 150000 
                          where s.Salary < 250000
                          select new
                          {
                              EmployeeName = s.EmpName,
                              Salary = s.Salary
                          };

        Console.WriteLine("Employee Whose Salary in Between 150000 and 250000:");

        foreach (var emp in QuerySyntax)
        {
            Console.WriteLine($"Name : {emp.EmployeeName}, Salary : {emp.Salary}");
        }

        Console.ReadKey();
    }
}

public class Employee
{

    public int EmpID { get; set; }
    public string EmpName { get; set; }
    public string City { get; set; }
    public int Salary { get; set; }

}
Output
Employee Whose Salary in Between 150000 and 250000:
Name : Moin, Salary : 210000
Name : Bill, Salary : 180000
Name : Ram, Salary : 200000
Example 2: Multiple Where Clause using Method Syntax
Example 2: Multiple Where Clause using Method Syntax 
using System;
using System.Linq;
using System.Collections.Generic;


public class Program
{
    public static void Main()
    {
        IList<Employee> employeelist = new List<Employee>() {
                new Employee() { EmpID = 1, EmpName = "John", City = "New York", Salary = 130000} ,
                new Employee() { EmpID = 2, EmpName = "Moin", City = "New Orleans", Salary = 210000 } ,
                new Employee() { EmpID = 3, EmpName = "Bill", City = "Seattle", Salary = 180000 } ,
                new Employee() { EmpID = 4, EmpName = "Ram" , City = "Delhi", Salary = 200000} ,
                new Employee() { EmpID = 5, EmpName = "Ron" , City = "San Jose", Salary = 150000 }
            };

        // LINQ Method Syntax to find out Employee 
        // Whose Salary in Between 150000 and 250000
        var MethodSyntax = employeelist
                           .Where(emp => emp.Salary > 150000).Where(emp => emp.Salary < 250000)
                           .Select(emp => new
                           {
                               EmployeeName = emp.EmpName,
                               Salary = emp.Salary,
                           });

        Console.WriteLine("Employee Whose Salary in Between 150000 and 250000:");

        foreach (var emp in MethodSyntax)
        {
            Console.WriteLine($"Name : {emp.EmployeeName}, Salary : {emp.Salary}");
        }

        Console.ReadKey();
    }
}

public class Employee
{

    public int EmpID { get; set; }
    public string EmpName { get; set; }
    public string City { get; set; }
    public int Salary { get; set; }

}
Output
Employee Whose Salary in Between 150000 and 250000:
Name : Moin, Salary : 210000
Name : Bill, Salary : 180000
Name : Ram, Salary : 200000

Example of Calling Method in Where Clause

In LINQ, you can also call any method that is similar to the Func parameter with one of the Where() method overloads. Below is the example of calling any method that matches the Func Parameter:

Example: Calling Method in Where Clause
using System;
using System.Linq;
using System.Collections.Generic;


public class Program
{
    public static void Main()
    {
        IList<Employee> employeelist = new List<Employee>() {
                new Employee() { EmpID = 1, EmpName = "John", City = "New York", Salary = 130000} ,
                new Employee() { EmpID = 2, EmpName = "Moin", City = "New Orleans", Salary = 210000 } ,
                new Employee() { EmpID = 3, EmpName = "Bill", City = "Seattle", Salary = 180000 } ,
                new Employee() { EmpID = 4, EmpName = "Ram" , City = "Delhi", Salary = 200000} ,
                new Employee() { EmpID = 5, EmpName = "Ron" , City = "San Jose", Salary = 150000 }
            };

        // LINQ Query Syntax to find out Employee 
        // Whose Salary in Between 150000 and 250000
        var QuerySyntax = from s in employeelist
                          where SalaryInBetween(s)
                          select new
                          {
                              EmployeeName = s.EmpName,
                              Salary = s.Salary
                          };

        Console.WriteLine("Employee Whose Salary in Between 150000 and 250000:");

        foreach (var emp in QuerySyntax)
        {
            Console.WriteLine($"Name : {emp.EmployeeName}, Salary : {emp.Salary}");
        }

        Console.ReadKey();
    }

    //Method similar to the Func parameter
    public static bool SalaryInBetween(Employee emp)
    {
        return emp.Salary > 150000 && emp.Salary < 250000;
    }
}

public class Employee
{

    public int EmpID { get; set; }
    public string EmpName { get; set; }
    public string City { get; set; }
    public int Salary { get; set; }

}
Output
Employee Whose Salary in Between 150000 and 250000:
Name : Moin, Salary : 210000
Name : Bill, Salary : 180000
Name : Ram, Salary : 200000

Learn Filtering Operator - OfType in the next tutorial