Friday, 20 December 2013

LINQ Examples 2

LINQ - Using Quantifiers like Any, Contains and All while Querying Objects
Quantifier operations return a Boolean value if some or all of the elements in a sequence satisfy a condition. In this article, we will see some common LINQ to Objects scenarios where we can use these operators.
There are 3 Quantifiers operations that can be used in LINQ:
All – used to determine whether all the elements in a sequence satisfy a condition.
Any - used to determine whether any elements in a sequence satisfy a condition.
Contains - used to determine whether a sequence contains a specified element
 
List<Department> dept = new List<Department>();
    dept.Add(new Department() { DeptID = 1, DeptName = "Marketing", Floor = 1 });
    dept.Add(new Department() { DeptID = 2, DeptName = "Sales", Floor = 2 });
    dept.Add(new Department() { DeptID = 3, DeptName = "Adminis", Floor = 3 });
    dept.Add(new Department() { DeptID = 4, DeptName = "Accounts", Floor = 3 });
    dept.Add(new Department() { DeptID = 5, DeptName = "HR", Floor = 3 });
 
    List<Employee> emp = new List<Employee>();
    emp.Add(new Employee() { EmpID = 1, DeptID = 1, EmpName = "Jack Nolas"});
    emp.Add(new Employee() { EmpID = 2, DeptID = 4, EmpName = "Mark Pine" });
    emp.Add(new Employee() { EmpID = 3, DeptID = 3, EmpName = "Sandra Simte" });
    emp.Add(new Employee() { EmpID = 4, DeptID = 4, EmpName = "Larry Lo"});
    emp.Add(new Employee() { EmpID = 5, DeptID = 3, EmpName = "Sudhir Panj" });
    emp.Add(new Employee() { EmpID = 6, DeptID = 2, EmpName = "Kathy K" });
    emp.Add(new Employee() { EmpID = 7, DeptID = 1, EmpName = "Kaff Joe" });
    emp.Add(new Employee() { EmpID = 8, DeptID = 1, EmpName = "Su Lie" });
 
Using ‘Any’ Quantifier in LINQ
This sample uses the ‘Any’ operator to list down the Departments that do not have Employees
C#
    var noEmp =
        from d in dept
        where !emp.Any(e => e.DeptID == d.DeptID)
        select new { dId = d.DeptID, dNm = d.DeptName };
 
    Console.WriteLine("Departments having no Employees");
    foreach (var empl in noEmp)
    {
        Console.WriteLine("Dept ID - " + empl.dId + ", Dept Name - " + empl.dNm);
 
    }
Output:
Any Operator
 
Using ‘Contains’ Quantifier in LINQ
The following example uses the ‘Contains’ quantifier to find the List of Departments having Employee Names starting with ‘S’
C#
     // Functionality Similar to IN operator
    var hasEmp = dept
         .Where(e => emp.Where(contact =>
         contact.EmpName.StartsWith("S"))
         .Select(d => d.DeptID)
         .Contains(e.DeptID));
 
    Console.WriteLine("/nList of Departments having Employee Names starting with S");
    foreach (var dpt in hasEmp)
    {
        Console.WriteLine("Dept ID - " + dpt.DeptID + ", Dept Name - " + dpt.DeptName);
    }
 
Output:
Contains Operator
Using ‘All’ Quantifier in LINQ
Using the ‘All’ operator, we can determine whether all employees have their names starting with ‘A’
 Console.WriteLine("Find if all Employees have their names starting with 'A'");
 bool chkName = emp.All(e =>
                    e.EmpName.StartsWith("A"));
 Console.WriteLine("Result : " + chkName);
 
 Console.ReadLine();

Output
All Operator
 
 
 
 

No comments:

Post a Comment