LINQ | Filtering Operator | where

21 May, 2019
Filtering operators are those operators which are used to filter the data according to the user requirement from the given data source or from the given sequence. For example, in an employee record, we want to get the data of the employees whose age in 21. So, we filter the record, according to their age. In LINQ, you can filter using the following operators:
WhereOfType
Where
OfType
Where operator filters the value according to the predicate function. Or in words, we can say it returns the values from the sequence based on the given condition or criteria. Where clause is not a mandatory clause in the query.
Where Clause in Query Syntax: The where clause is used to filter the query according to the given condition. You can provide a condition to where clause using lambda expression or by using Func delegate type. Where clause supports query syntax in both C# and VB.Net languages. Query Syntax of Where clause is as shown in the below example.
Example:
// C# program to find the name of the // employees whose id less than equals// to 211using System;using System.Linq;using System.Collections.Generic;  // Employee detailspublic class Employee {      public int emp_id    {        get;        set;    }      public string emp_name    {        get;        set;    }      public string emp_gender    {        get;        set;    }      public string emp_hire_date    {        get;        set;    }      public int emp_salary    {        get;        set;    }}  class GFG {      // Main method    static public void Main()    {        List<Employee> emp = new List<Employee>() {                new Employee() { emp_id = 209, emp_name = "Anjita", emp_gender = "Female",                                         emp_hire_date = "12/3/2017", emp_salary = 20000 },                                                           new Employee() { emp_id = 210, emp_name = "Soniya", emp_gender = "Female",                                         emp_hire_date = "22/4/2018", emp_salary = 30000 },                                                           new Employee() { emp_id = 211, emp_name = "Rohit", emp_gender = "Male",                                      emp_hire_date = "3/5/2016", emp_salary = 40000 },                                                        new Employee() { emp_id = 212, emp_name = "Supriya", emp_gender = "Female",                                          emp_hire_date = "4/8/2017", emp_salary = 40000 },                                                            new Employee() { emp_id = 213, emp_name = "Anil", emp_gender = "Male",                                     emp_hire_date = "12/1/2016", emp_salary = 40000 },                                                      new Employee() { emp_id = 214, emp_name = "Anju", emp_gender = "Female",                                      emp_hire_date = "17/6/2015", emp_salary = 50000 },        };          // Query to find the name of the         // employees whose id is less         // than equals to 211 Using where        // clause in Query Syntax        var res = from e in emp                    where e.emp_id                <= 211 select e.emp_name;          foreach(var val in res)        {            Console.WriteLine("Employee Name: {0}", val);        }    }}
Employee Name: Anjita
Employee Name: Soniya
Employee Name: Rohit

Where Clause in Method Syntax: In Method syntax, Where clause works as a method. It present in both the Queryable and Enumerable class. It supports method syntax in both C# and VB.NET languages. The use of the Where method is shown in the below example.
Example:
// C# program to find the name of the employees// whose salary is greater than 40000using System;using System.Linq;using System.Collections.Generic;      // Employee detailspublic class Employee {          public int emp_id{get; set;}    public string emp_name{get; set;}    public string emp_gender{get; set;}    public string emp_hire_date{get; set;}    public int emp_salary{get; set;}                    }      public class GFG{              // Main method    static public void Main (){              List<Employee> emp = new List<Employee>(){                  new Employee(){emp_id = 209, emp_name = "Anjita", emp_gender = "Female",                               emp_hire_date = "12/3/2017", emp_salary = 20000},                                         new Employee(){emp_id = 210, emp_name = "Soniya", emp_gender = "Female",                               emp_hire_date = "22/4/2018", emp_salary = 30000},                                         new Employee(){emp_id = 211, emp_name = "Rohit", emp_gender = "Male",                             emp_hire_date = "3/5/2016", emp_salary = 40000},                                       new Employee(){emp_id = 212, emp_name = "Supriya", emp_gender = "Female",                                 emp_hire_date = "4/8/2017", emp_salary = 80000},                                           new Employee(){emp_id = 213, emp_name = "Anil", emp_gender = "Male",                           emp_hire_date = "12/1/2016", emp_salary = 60000},                                     new Employee(){emp_id = 214, emp_name = "Anju", emp_gender = "Female",                             emp_hire_date = "17/6/2015", emp_salary = 50000},    };              // finding the name of the employees    // whose salary is greater than 40000    // Using Where method    var res = emp.Where(a=>a.emp_salary > 40000);                      foreach(var val in res)    {        Console.WriteLine("Employee Name: {0} ", val.emp_name);    }          }}
Employee Name: Supriya 
Employee Name: Anil 
Employee Name: Anju 

Multiple Where Clause: In LINQ, you are allowed to use multiple where clause or Where method in the single query. As shown in the below example:
Example 1:
// Query to find the name of the // employees whose id is less than// equals to 211 and whose salary // is less than 50000 Using multiple// where clause in Query Syntaxvar res = from e in emp              where e.emp_id          <= 211 where e.emp_salary < 50000 select e.emp_name;
Example 2:
// finding the name of the employees// whose salary is greater than 40000 and// whose id is less than 214// Using multiple Where method in single queryvar res = emp.Where(a => a.emp_salary > 40000).Where(e = > e.emp_id < 214);
CSharp LINQ
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
C# Dictionary with examples
Introduction to .NET Framework
C# | Delegates
C# | Multiple inheritance using interfaces
Differences Between .NET Core and .NET Framework
C# | Method Overriding
C# | Data Types
C# | Constructors
C# | String.IndexOf( ) Method | Set - 1
C# | Class and Object