Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Quantifier Operators in Linq

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 531
    Comment on it

    The quantifier operators  analyse a collection of items on some condition and  return a boolean value if any or all the items in collection satisfy a condition.

     

    In linq, We have three types of Quantifier Operators:-
    1. All
    2. Any
    3. Contain
    These Quantifier Operators are not supports a query syntax.


    1. All:- It is used to check whether all the items in a sequence satisfy a condition or not. If all the items satisfy a condition then it returns true, Otherwise it returns false.


    Example:-

    IList<Employee> empList = new List<Employee>() { 
            new Employee() { EmpID = Guid.NewGuid().ToString(), EmpName = "John", Age = 24 } ,
            new Employee() { EmpID = Guid.NewGuid().ToString(), EmpName = "Steve",  Age = 24 } ,
            new Employee() { EmpID = Guid.NewGuid().ToString(), EmpName = "Bill",  Age = 30 } ,
            new Employee() { EmpID = Guid.NewGuid().ToString(), EmpName = "Ram" , Age = 28 } ,
            new Employee() { EmpID = Guid.NewGuid().ToString(), EmpName = "Ron" , Age = 35} 
        };
    
    bool result = empList .All(e => e.Age > 20 && e.Age < 30);
    Console.WriteLine(result);
    
    

     

    Output:-

    False

    Above code will returns a false because all employees are not come between the age of 20-30.

     

    2. Any:-  It is used to check whether any item in a sequence satisfy a condition or not. If any  item satisfy a condition then it returns true, Otherwise it returns false.


    Example:-

    IList<Employee> empList = new List<Employee>() { 
             new Employee() { EmpID = Guid.NewGuid().ToString(), EmpName = "John", Age = 24 } ,
            new Employee() { EmpID = Guid.NewGuid().ToString(), EmpName = "Steve",  Age = 24 } ,
            new Employee() { EmpID = Guid.NewGuid().ToString(), EmpName = "Bill",  Age = 25 } ,
            new Employee() { EmpID = Guid.NewGuid().ToString(), EmpName = "Ram" , Age = 31 } ,
            new Employee() { EmpID = Guid.NewGuid().ToString(), EmpName = "Ron" , Age = 35}
        };
    bool result = empList .Any(e => e.Age > 24 && e.Age < 30);
    Console.WriteLine(result);
    

     

    Output:-

    True
    

     

    Above code will returns a true because one of the employee satisfy the condition.

    3. Contains:- It is used to check whether a sequence of an items contains a specified item or not. If the specified item exists in sequence of items  then it returns true, otherwise it returns false.

    Example:-

    IList<int> intList = new List<int>() { 1, 2, 3, 4, 5 };
    bool result = intList.Contains(10);  
    Console.Write(result );
    

     

    Output:-

    False

    Above example is used only with the primitive data types. It will not work with a custom class.


    For example:-

       IList<Employee> empList = new List<Employee>() {
       new Employee() { EmpID = Guid.NewGuid().ToString(), EmpName = "John", Age = 24 } ,
       new Employee() { EmpID = Guid.NewGuid().ToString(), EmpName = "Steve",  Age = 24 } ,
       new Employee() { EmpID = Guid.NewGuid().ToString(), EmpName = "Bill",  Age = 25 } ,
        new Employee() { EmpID = Guid.NewGuid().ToString(), EmpName = "Ram" , Age = 31 } ,
        new Employee() { EmpID = Guid.NewGuid().ToString(), EmpName = "Ron" , Age = 35}
     };
       Employee emp = new Employee(){ EmpName = "Bill"};
       bool result = empList.Contains(std); 
       Console.Write(result);
    

    The above example returns false because contains compares the reference of an object not a value of an object. To compare values of the object we need to create a class which implements a  IEqualityComparer interface.


    See the below code:-

    class EmpComparer : IEqualityComparer<Employee>
    {
            public bool Equals(Employee x, Employeey)
            {
                if ( x.EmpName.ToLower() == y.EmpName.ToLower() )
                      {
                             return true;
                      }
                return false;
            }
            public int GetHashCode(Employee empobj)
            {
                return empobj.GetHashCode();
            }
    }
    

    In the above code we overloads a Contains extension method that takes a second parameter of IEqualityComparer type.


    Code:-

    IList<Employee> empList = new List<Employee>() {
       new Employee() { EmpID = Guid.NewGuid().ToString(), EmpName = "John", Age = 24 } ,
       new Employee() { EmpID = Guid.NewGuid().ToString(), EmpName = "Steve",  Age = 24 } ,
       new Employee() { EmpID = Guid.NewGuid().ToString(), EmpName = "Bill",  Age = 25 } ,
        new Employee() { EmpID = Guid.NewGuid().ToString(), EmpName = "Ram" , Age = 31 } ,
        new Employee() { EmpID = Guid.NewGuid().ToString(), EmpName = "Ron" , Age = 35}
     };
    Employee emp = new Employee(){ EmpName = "Bill"};
    bool result = empList.Contains(emp, new EmpComparer()); 
    

    Now, the above code returns a true result.

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: