Sunday, 2 July 2017

LINQ Quantifier Operators - any, all and contains with examples in C#

In this article, I am going to explain you LINQ Quantifier operators in C#. Quantifier operators return boolean value i.e. true or false when some or all the elements within a sequence satisfy the conditions. Below are the 3 types of quantifier operators.
Any - It is used to check whether any elements in a sequence satisfy a condition.
All – It is used to check whether all the elements in a sequence satisfy a condition.
Contains - It is used to check whether a sequence contains a specified element.

Let's understand the these operators with examples. I have created a string array which contains random words and created two list of type Employee and Department as shown below.

class Employee
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Salary { get; set; }
    public int DeptId { get; set; }
}
class Department
{
    public int Id { get; set; }
    public string Name { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        // string array
        string[] words = { "believe", "relief", "receipt", "field" };

        // list of type Employee
        List<Employee> employee = new List<Employee>(){
                new Employee { Id=1, Name = "Rahul", Salary=50000, DeptId=101},
                new Employee { Id=2, Name = "John", Salary=10000, DeptId=102},
                new Employee { Id=3, Name = "Mike", Salary=4000, DeptId=103},
                new Employee { Id=4, Name = "Mary", Salary=75000, DeptId=101},
                new Employee { Id=5, Name = "Sachin", Salary=6500, DeptId=102},
                new Employee { Id=6, Name = "Deepak", Salary=80000, DeptId=103},
                new Employee { Id=7, Name = "Mithoon", Salary=10000, DeptId=101},
                new Employee { Id=8, Name = "Hrithik", Salary=72000, DeptId=102},
                new Employee { Id=9, Name = "Savio", Salary=10000, DeptId=103},
                new Employee { Id=10, Name = "Yogesh", Salary=90000, DeptId=101}
            };
        // list of type Department
        List<Department> department = new List<Department>(){
                new Department { Id=101, Name = "IT"},
                new Department { Id=102, Name = "HR"}, 
                new Department { Id=103, Name = "Payroll"}, 
                new Department { Id=104, Name = "Sales"}, 
                new Department { Id=105, Name = "Production"},
            };
    }
}

Using Any Quantifier Operator In LINQ

Example 1

This example uses 'any' operator to check whether the any word within array contains sub-string 'ei'.

bool flag = words.Any(w => w.Contains("ei"));
if (flag)
{
    Console.WriteLine("There is a word containing 'ei'.");
}
else
{
    Console.WriteLine("There is no word that containing 'ei'.");
}
Console.ReadKey();

/*
OUTPUT:
There is a word containing 'ei'
*/

Example 2

This example uses 'any' operator to check any word having length greater than 6.

bool flag  = words.Any(w => w.Length > 6);
if (flag)
{
    Console.WriteLine("There is a word having lengh greater than 6.");
}
else
{
    Console.WriteLine("There is no word having lengh greater than 6.");
}
Console.ReadKey();

/*
OUTPUT:
There is a word having lengh greater than 6.
*/

Example 3

This example uses 'any' operator to list the departments that do not have employees.

var data = from dp in department
           where !employee.Any(e => e.DeptId == dp.Id)
           select new { dId = dp.Id, dNane = dp.Name }; // projecting into anonymous type
foreach (var d in data)
{
    Console.WriteLine("Dept Id - " + d.dId + ", Dept Name - " + d.dNane);

}
Console.ReadKey();

/*
OUTPUT:
    Dept Id - 104, Dept Name - Sales
    Dept Id - 105, Dept Name - Production
*/

Using All Quantifier Operator In LINQ

Example 1

This example uses 'all' operator to check whether all words within array contains a letter 'e'.

bool flag = words.All(w => w.Contains("e"));
if (flag)
{
    Console.WriteLine("All the words contain letter 'e'.");
}
else
{
    Console.WriteLine("All the word do not contain letter 'e'.");
}
Console.ReadKey();

/*
OUTPUT:
    All the words contain letter 'e'.
*/

Example 2

This example uses 'all' operator to check whether all the employees name starts with a letter 'Y'.

bool flag = employee.All(e => e.Name.StartsWith("Y"));
if (flag)
{
    Console.WriteLine("All the employees Name starts with 'Y'.");
}
else
{
    Console.WriteLine("Not all employees Name starts with 'Y'.");
}

Console.ReadLine();
/*
OUTPUT:
  Not all employees Name starts with 'Y'.
*/

Using Contains Quantifier Operator In LINQ

Example 1

This example uses 'contains' operator to list down all the department having employees name starts with 'M'.

var data = department.Where(d => employee.Where(e => e.Name.StartsWith("M")).
    Select(e1 => e1.DeptId).Contains(d.Id));
 
Console.WriteLine("List of Departments having Employee Names starting with 'M'.");
foreach (var d in data)
{
    Console.WriteLine("Dept Id - " + d.Id + ", Dept Name - " + d.Name);
}
Console.ReadLine();
/*
OUTPUT:
  List of Departments having Employee Names starting with 'M'.
  Dept Id - 101, Dept Name - IT
  Dept Id - 103, Dept Name - Payroll
*/
Share:

0 comments:

Post a Comment

Email Subscription

Subscribe to our newsletter to get the latest articles directly into your inbox

Blog Archive

BUY FROM AMAZON