Hello friends, In this tutorial, you are going to understand what is Where clause Filtering operator in Linq. The Where operator is used to filter a collection based on the given criteria expression and return a new collection.

Where Clause Filtering Operator in Linq:

The Where operator is used to filter a collection based on the given criteria expression and return a new collection. Where Clause is not mandatory for LINQ statement but can be used to limit the number of records. It can also be used for extracting those records which we don’t want to select, update or delete. The where clause can be placed anywhere except it cannot be the first or last clause.

Example:

We are going to understand where clause operator using two types of examples.

  • Using where clause in Query Syntax.
  • Using Where extension method in Method Syntax.

1- Using where clause in Query Syntax:

Example 1 Select specific record:

I want to get details of Employee with Employee Code “E001”.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

using System.Runtime.Serialization.Json;
namespace StaticClassDemo
{

public class Employee 
{
public string EmployeeCode { get; set; }
public string EmployeeName { get; set; }
public string Gender { get; set; }
public string Designation { get; set; }
public double Salary { get; set; }
}
class Program
{
public static List<Employee> DataSource()
{
List<Employee> lstEmployee = new List<Employee>();
Employee objEmployee1 = new Employee() { EmployeeCode = "E001", EmployeeName = "Ayush Singh", Gender = "Male", Designation = "Software Engineer", Salary = 30000 };
Employee objEmployee2 = new Employee() { EmployeeCode = "E002", EmployeeName = "Ritwik Nigam", Gender = "Male", Designation = "Software Engineer", Salary = 30000 };
Employee objEmployee3 = new Employee() { EmployeeCode = "E003", EmployeeName = "Nikhil Bakshi", Gender = "Male", Designation = "Team Lead", Salary = 60000 };
Employee objEmployee4 = new Employee() { EmployeeCode = "E004", EmployeeName = "Nidhi Verma", Gender = "Female", Designation = "HR", Salary = 40000 };
Employee objEmployee5 = new Employee() { EmployeeCode = "E005", EmployeeName = "Ayushman Lakhotia", Gender = "Male", Designation = "Project Manager", Salary = 80000 };
Employee objEmployee6 = new Employee() { EmployeeCode = "E006", EmployeeName = "Neha Sharma", Gender = "Female", Designation = "Graphic Designer", Salary = 25000 };
lstEmployee.Add(objEmployee1);
lstEmployee.Add(objEmployee2);
lstEmployee.Add(objEmployee3);
lstEmployee.Add(objEmployee4);
lstEmployee.Add(objEmployee5);
lstEmployee.Add(objEmployee6);
return lstEmployee;
}
 
static void Main(string[] args)
{
 
var vEmployee=from employee in DataSource() where employee.EmployeeCode=="E001"
select employee;
foreach (Employee objEmployee in vEmployee)
{
Console.WriteLine("Employee Name:{0}", objEmployee.EmployeeName);
Console.WriteLine("Gender:{0}", objEmployee.Gender);
Console.WriteLine("Designation:{0}", objEmployee.Designation);
Console.WriteLine("Salary:{0}", objEmployee.Salary);
}

Console.ReadLine(); 
}
 
}
}

Output:

Employee Name:Ayush Singh
Gender:Male
Designation:Software Engineer
Salary:30000

Example 2 Where clause with single condition:

I want to select Employee having salary greater than 30000
var vEmployee=from employee in DataSource() where employee.Salary>30000
select employee;
foreach (Employee objEmployee in vEmployee)
{
Console.WriteLine("Employee Name:{0}", objEmployee.EmployeeName);
Console.WriteLine("Gender:{0}", objEmployee.Gender);
Console.WriteLine("Designation:{0}", objEmployee.Designation);
Console.WriteLine("Salary:{0}", objEmployee.Salary);
Console.WriteLine("------------------------------------------");

}

Output:

Employee Name:Nikhil Bakshi
Gender:Male
Designation:Team Lead
Salary:60000
------------------------------------------
Employee Name:Nidhi Verma
Gender:Female
Designation:HR
Salary:40000
------------------------------------------
Employee Name:Ayushman Lakhotia
Gender:Male
Designation:Project Manager
Salary:80000
------------------------------------------

EXAMPLE 3 WHERE CLAUSE WITH multiple CONDITION:

I want to select Employee having salary greater than 30000 and less than 50000

var vEmployee=from employee in DataSource() where employee.Salary>30000 && employee.Salary<50000
select employee;
foreach (Employee objEmployee in vEmployee)
{
Console.WriteLine("Employee Name:{0}", objEmployee.EmployeeName);
Console.WriteLine("Gender:{0}", objEmployee.Gender);
Console.WriteLine("Designation:{0}", objEmployee.Designation);
Console.WriteLine("Salary:{0}", objEmployee.Salary);
Console.WriteLine("------------------------------------------");

}

Output:

Employee Name:Nidhi Verma
Gender:Female
Designation:HR
Salary:40000

Example 4 Condition On String Value:

I want to select Employee where Employee Name Starts With “A”:

var vEmployee = from employee in DataSource()
where employee.EmployeeName.StartsWith("A")
select employee;
foreach (Employee objEmployee in vEmployee)
{
Console.WriteLine("Employee Name:{0}", objEmployee.EmployeeName);
Console.WriteLine("Gender:{0}", objEmployee.Gender);
Console.WriteLine("Designation:{0}", objEmployee.Designation);
Console.WriteLine("Salary:{0}", objEmployee.Salary);
Console.WriteLine("------------------------------------------");

}

Output:

Employee Name:Ayush Singh
Gender:Male
Designation:Software Engineer
Salary:30000
------------------------------------------
Employee Name:Ayushman Lakhotia
Gender:Male
Designation:Project Manager
Salary:80000
------------------------------------------

Example 5 Using Func Type Delegate:

I want to select Employees having designation Team Lead or Project Manager. For this I am creating a Func Type Delegate.

Func<Employee, bool> IsTLOrPM = delegate(Employee objEmployee)
{
return objEmployee.Designation == "Team Lead" || objEmployee.Designation == "Project Manager";
};

var vEmployee = from employee in DataSource()
where IsTLOrPM(employee)
select employee;
foreach (Employee objEmployee in vEmployee)
{
Console.WriteLine("Employee Name:{0}", objEmployee.EmployeeName);
Console.WriteLine("Gender:{0}", objEmployee.Gender);
Console.WriteLine("Designation:{0}", objEmployee.Designation);
Console.WriteLine("Salary:{0}", objEmployee.Salary);
Console.WriteLine("------------------------------------------");

}

Output:

Employee Name:Nikhil Bakshi
Gender:Male
Designation:Team Lead
Salary:60000
------------------------------------------
Employee Name:Ayushman Lakhotia
Gender:Male
Designation:Project Manager
Salary:80000
------------------------------------------

Using Where extension method in Method Syntax:

I am going to write query with where extension method in method syntax for the above example

EXAMPLE 1 SELECT SPECIFIC RECORD:

I want to get details of Employee with Employee Code “E001”.

var vEmployee = DataSource().Where(e => e.EmployeeCode == "E001");
foreach (Employee objEmployee in vEmployee)
{
Console.WriteLine("Employee Name:{0}", objEmployee.EmployeeName);
Console.WriteLine("Gender:{0}", objEmployee.Gender);
Console.WriteLine("Designation:{0}", objEmployee.Designation);
Console.WriteLine("Salary:{0}", objEmployee.Salary);
Console.WriteLine("------------------------------------------");

}

EXAMPLE 2 WHERE CLAUSE WITH SINGLE CONDITION:

I want to select Employee having salary greater than 30000

var vEmployee = DataSource().Where(e => e.Salary >30000);
foreach (Employee objEmployee in vEmployee)
{
Console.WriteLine("Employee Name:{0}", objEmployee.EmployeeName);
Console.WriteLine("Gender:{0}", objEmployee.Gender);
Console.WriteLine("Designation:{0}", objEmployee.Designation);
Console.WriteLine("Salary:{0}", objEmployee.Salary);
Console.WriteLine("------------------------------------------");

}

EXAMPLE 3 WHERE CLAUSE WITH MULTIPLE CONDITION:

I want to select Employee having a salary greater than 30000 and less than 50000

var vEmployee = DataSource().Where(e => e.Salary >30000 && e.Salary<=50000);
foreach (Employee objEmployee in vEmployee)
{
Console.WriteLine("Employee Name:{0}", objEmployee.EmployeeName);
Console.WriteLine("Gender:{0}", objEmployee.Gender);
Console.WriteLine("Designation:{0}", objEmployee.Designation);
Console.WriteLine("Salary:{0}", objEmployee.Salary);
Console.WriteLine("------------------------------------------");

}

EXAMPLE 4 CONDITION ON STRING VALUE:

I want to select Employee where Employee Name Starts With “A”:

var vEmployee = DataSource().Where(e => e.EmployeeName.StartsWith("A"));
foreach (Employee objEmployee in vEmployee)
{
Console.WriteLine("Employee Name:{0}", objEmployee.EmployeeName);
Console.WriteLine("Gender:{0}", objEmployee.Gender);
Console.WriteLine("Designation:{0}", objEmployee.Designation);
Console.WriteLine("Salary:{0}", objEmployee.Salary);
Console.WriteLine("------------------------------------------");

}

EXAMPLE 5 USING FUNC TYPE DELEGATE:

I want to select Employees having designation Team Lead or Project Manager. For this I am creating a Func Type Delegate.

Func<Employee, bool> IsTLOrPM = delegate(Employee objEmployee)
{
return objEmployee.Designation == "Team Lead" || objEmployee.Designation == "Project Manager";
};

var vEmployee = DataSource().Where(e => IsTLOrPM(e));
foreach (Employee objEmployee in vEmployee)
{
Console.WriteLine("Employee Name:{0}", objEmployee.EmployeeName);
Console.WriteLine("Gender:{0}", objEmployee.Gender);
Console.WriteLine("Designation:{0}", objEmployee.Designation);
Console.WriteLine("Salary:{0}", objEmployee.Salary);
Console.WriteLine("------------------------------------------");

}

View More:

Conclusion:

I hope you would love this. Please don’t hesitate to comment for any technical issues. Your feedback, comments, and suggestions are appreciated.

Thank You

Leave a Comment