📜  LINQ |过滤运算符|在哪里

📅  最后修改于: 2021-05-29 20:56:15             🧑  作者: Mango

过滤运算符符是用于根据用户要求从给定数据源或给定序列中过滤数据的那些运算符。例如,在员工记录中,我们希望获取年龄在21岁以下的员工的数据。因此,我们根据他们的年龄过滤记录。在LINQ中,可以使用以下运算符进行过滤:

  1. 在哪里
  2. 类型

运算符在哪里

运算符根据谓词函数对值进行过滤的位置。换句话说,我们可以说它根据给定的条件或条件从序列中返回值。 where子句不是查询中的强制性子句。

查询语法中的where子句 where子句用于根据给定条件过滤查询。您可以使用lambda表达式或Func委托类型为where子句提供条件。 Where子句支持C#和VB.Net语言的查询语法。 Where子句的查询语法如下例所示。

例子:

// 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 emp = new List() {
                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子句在Method语法中,Where子句用作方法。它同时存在于Queryable和Enumerable类中。它支持C#和VB.NET语言的方法语法。下例显示了Where方法的使用。

例子:

// 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 emp = new List(){
          
        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 

多个Where子句:在LINQ中,允许在单个查询中使用多个where子句或Where方法。如下例所示:

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

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