LINQ | Quantifier Operator | Contains

In LINQ, quantifier operators are used to returning a boolean value which shows that whether some or all elements satisfies the given condition. The Standard Query Operator supports 3 different types of quantifier operators:

  1. All
  2. Any
  3. Contains

Contains Operator

The Contains operator is used to check whether the given sequence or collection contains the specified element or not. It will return true if the given sequence contains the specified element, otherwise, return false. It is overloaded in two different types:

  • Contains<TSource>(IEnumerable<TSource>, TSource): This method is used to check whether a sequence contains a specified element by using the default equality comparer.
  • Contains<TSource>(IEnumerable<TSource>, TSource, IEqualityComparer<TSource>): This method is used to check whether a sequence contains a specified element by using a specified IEqualityComparer.

Important Points:



  • It does not support query syntax in C# and VB.Net languages.
  • It support method syntax in both C# and VB.Net languages.
  • It present in both the Queryable and Enumerable class.
  • It will throw ArgumentNullException if the given source is null.
  • It does not return value instead of this it return true or false.
  • The return type of this operator is System.Boolean.
  • You can use custom class that derives IEqualityOperator with Contains to check for the object in the given collection.

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

Result: True
Result: False

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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<Employee>
// To compare the details of the new 
// employee to the details of the old list
public class EmpCompar : IEqualityComparer<Employee> {
      
    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<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},
        };
  
        // 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);
    }
}

chevron_right


Output:

Employee Found?: False


My Personal Notes arrow_drop_up

Recommended Posts:



    Article Tags :

    Be the First to upvote.


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