📜  LINQ |量词运算符|包含

📅  最后修改于: 2021-05-29 23:41:25             🧑  作者: Mango

在LINQ中,量词运算符用于返回一个布尔值,该值表明是否某些或所有元素都满足给定条件。标准查询运算符支持3种不同类型的量词运算符:

  1. 全部
  2. 任何
  3. 包含

包含运算符

Contains运算符用于检查给定的序列或集合是否包含指定的元素。如果给定序列包含指定的元素,它将返回true,否则返回false。它以两种不同的类型重载:

  • Contains (IEnumerable ,TSource):此方法用于通过使用默认的相等比较器检查序列是否包含指定的元素。
  • Contains (IEnumerable ,TSource,IEqualityComparer ):此方法用于通过使用指定的IEqualityComparer检查序列是否包含指定的元素。

重要事项:

  • 它不支持C#和VB.Net语言的查询语法。
  • 它支持C#和VB.Net语言的方法语法。
  • 它同时存在于Queryable和Enumerable类中。
  • 如果给定的源为null,它将抛出ArgumentNullException。
  • 它不返回值,而是返回true或false。
  • 该运算符的返回类型为System.Boolean
  • 您可以使用通过Includes派生IEqualityOperator的自定义类来检查给定集合中的对象。

范例1:

// C# program to illustrate the
// use of Contains operator
using System;
using System.Linq;
using System.Collections.Generic;
  
class GFG {
  
    static public void Main()
    {
  
        // Data source
        int[] sequence1 = {34, 56, 77, 88,
                          99, 10, 23, 46};
  
        string[] sequence2 = {"Geek", "Geeks123",
                                "GeeksforGeeks"};
  
        // Check the sequence1 contain 10
        // Using Contains operator
        var result1 = sequence1.Contains(10);
  
        Console.WriteLine("Result: {0}", result1);
  
        // Check the sequence2 contain "qq"
        // Using Contains operator
        var result2 = sequence2.Contains("123Geek");
  
        Console.WriteLine("Result: {0}", result2);
    }
}
输出:
Result: True
Result: False

范例2:

// C# program to check the new employee
// is found in the given old data list
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;
    }
}
  
// the EmpCompar class implements
// IEqualityComparer
// To compare the details of the new 
// employee to the details of the old list
public class EmpCompar : IEqualityComparer {
      
    public bool Equals(Employee a, Employee b)
    {
        if (a.emp_id == b.emp_id && a.emp_name == b.emp_name &&
                                a.emp_gender == b.emp_gender && 
                          a.emp_hire_date == b.emp_hire_date && 
                                  a.emp_salary == b.emp_salary)
            return true;
  
        return false;
    }
  
    public int GetHashCode(Employee obj)
    {
        return obj.GetHashCode();
    }
}
  
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},
        };
  
        // New employee
        Employee emp1 = new Employee() {emp_id = 215, emp_name = "Anu",
                    emp_gender = "Female", emp_hire_date = "18/6/2015",
                                                   emp_salary = 50000};
  
        // Query to check the new employee is 
        // found in the given old data list
        // Using Contain operator
        var res = emp.Contains(emp1, new EmpCompar());
        Console.WriteLine("Employee Found?: {0}", res);
    }
}
输出:
Employee Found?: False