LINQ | Filtering Operator | where

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:

  1. Where
  2. OfType

Where Operator

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find the name of the 
// employees whose id less than equals
// to 211
using System;
using System.Linq;
using System.Collections.Generic;
  
// Employee details
public 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);
        }
    }
}

chevron_right


Output:

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find the name of the employees
// whose salary is greater than 40000
using System;
using System.Linq;
using System.Collections.Generic;
      
// Employee details
public 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);
    }
      
    }
}

chevron_right


Output:

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 Syntax
var res = from e in emp
              where e.emp_id
          <= 211 where e.emp_salary < 50000 select e.emp_name;

chevron_right


Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 query
var res = emp.Where(a => a.emp_salary > 40000).Where(e = > e.emp_id < 214);

chevron_right




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.