Author : Rahul
Last Modified : 15-Jul-2021
Complexity : Beginner
Quantifier Operator in LINQ
Quantifier Operator in LINQ is used to return a Boolean value (True or False) if some or all elements within a collection satisfy the given condition. There are different Quantifier Operator are available in LINQ as follow:
- ANY
- ALL
ANY Operator
ANY operator is used to return true if any element within the collection satisfy the given condition. Any keyword is used for this purpose. Below are some examples in this category.
Query to find whether any number in the array is greater than 10.
class Program
{
static void Main(string[] args)
{
int[] numbers = { 3, 6, 9, 12, 15 };
//Linq Query
var result_Linq = (from p in numbers where p > 10 select p).Any();
//Lambda Expression
var result_Lambda = numbers.Any(p => p > 10);
//Display Linq Query Result
Console.WriteLine($"Any Number greater than 10 (Using Linq): {result_Linq}");
//Display Lambda Expression Result
Console.WriteLine($"Any Number greater than 10 (Using Lambda): {result_Lambda}");
Console.Read();
}
}
Output
Any Number greater than 10 (Using Linq): True
Any Number greater than 10 (Using Lambda): True
Query to find whether any employee in the list of employees having salary is greater than 50000.
public class Employee
{
public string Emp_Name { get; set; }
public string Emp_Department { get; set; }
public int Emp_Salary { get; set; }
}
class Program
{
static void Main(string[] args)
{
List<Employee> employees = GetEmployeeList();
//Linq Query
var result_Linq = (from p in employees where p.Emp_Salary > 50000 select p).Any();
//Lambda Expression
var result_Lambda = employees.Any(p => p.Emp_Salary > 50000);
//Display Linq Query Result
Console.WriteLine($"Any Employee with salary greater than 50000 (Using Linq): {result_Linq}");
//Display Lambda Expression Result
Console.WriteLine($"Any Employee with salary greater than 50000 (Using Linq): {result_Lambda}");
Console.Read();
}
/// <summary>
/// Method to get employee list
/// </summary>
/// <returns></returns>
private static List<Employee> GetEmployeeList()
{
List<Employee> employees = new List<Employee>();
employees.Add(new Employee() { Emp_Name = "Rahul", Emp_Salary = 70000, Emp_Department = "IT" });
employees.Add(new Employee() { Emp_Name = "Amit", Emp_Salary = 20000, Emp_Department = "HR" });
employees.Add(new Employee() { Emp_Name = "Rajesh", Emp_Salary = 40000, Emp_Department = "HR" });
employees.Add(new Employee() { Emp_Name = "Suresh", Emp_Salary = 60000, Emp_Department = "IT" });
employees.Add(new Employee() { Emp_Name = "Nitish", Emp_Salary = 80000, Emp_Department = "Account" });
employees.Add(new Employee() { Emp_Name = "Neeraj", Emp_Salary = 40000, Emp_Department = "Account" });
return employees;
}
}
Output
Any Employee with salary greater than 50000 (Using Linq): True
Any Employee with salary greater than 50000 (Using Lambda): True
Query to find grouped a list of employees only for department having at least one employee whose salary is greater than 50000.
public class Employee
{
public string Emp_Name { get; set; }
public string Emp_Department { get; set; }
public int Emp_Salary { get; set; }
}
class Program
{
static void Main(string[] args)
{
List<Employee> employees = GetEmployeeList();
//Linq Query
var result_Linq = from p in employees group p by p.Emp_Department into g
where g.Any(q => q.Emp_Salary > 50000)
select new { Department = g.Key, Employees = g };
//Lambda Expression
var result_Lambda = employees.GroupBy(p => p.Emp_Department).
Where(g => g.Any(q => q.Emp_Salary > 50000)).
Select(q => new { Department = q.Key, Employees = q });
//Display Linq Query Result
Console.WriteLine("Any Employee with salary greater than 50000 (Using Linq):");
foreach(var item in result_Linq)
{
Console.WriteLine($"Department Name: {item.Department}");
foreach(var employee in item.Employees)
{
Console.WriteLine($"Employee Name: {employee.Emp_Name}, Employee Department: {employee.Emp_Department}, Employee Salary: {employee.Emp_Salary}");
}
}
//Display Lambda Expression Result
Console.WriteLine("Any Employee with salary greater than 50000 (Using Lambda):");
foreach (var item in result_Linq)
{
Console.WriteLine($"Department Name: {item.Department}");
foreach (var employee in item.Employees)
{
Console.WriteLine($"Employee Name: {employee.Emp_Name}, Employee Department: {employee.Emp_Department}, Employee Salary: {employee.Emp_Salary}");
}
}
Console.Read();
}
/// <summary>
/// Method to get employee list
/// </summary>
/// <returns></returns>
private static List<Employee> GetEmployeeList()
{
List<Employee> employees = new List<Employee>();
employees.Add(new Employee() { Emp_Name = "Rahul", Emp_Salary = 70000, Emp_Department = "IT" });
employees.Add(new Employee() { Emp_Name = "Amit", Emp_Salary = 20000, Emp_Department = "HR" });
employees.Add(new Employee() { Emp_Name = "Rajesh", Emp_Salary = 40000, Emp_Department = "HR" });
employees.Add(new Employee() { Emp_Name = "Suresh", Emp_Salary = 60000, Emp_Department = "IT" });
employees.Add(new Employee() { Emp_Name = "Nitish", Emp_Salary = 80000, Emp_Department = "Account" });
employees.Add(new Employee() { Emp_Name = "Neeraj", Emp_Salary = 40000, Emp_Department = "Account" });
return employees;
}
}
Output
Any Employee with salary greater than 50000 (Using Linq):
Department Name: IT
Employee Name: Rahul, Employee Department: IT, Employee Salary: 70000
Employee Name: Suresh, Employee Department: IT, Employee Salary: 60000
Department Name: Account
Employee Name: Nitish, Employee Department: Account, Employee Salary: 80000
Employee Name: Neeraj, Employee Department: Account, Employee Salary: 40000
Any Employee with salary greater than 50000 (Using Lambda):
Department Name: IT
Employee Name: Rahul, Employee Department: IT, Employee Salary: 70000
Employee Name: Suresh, Employee Department: IT, Employee Salary: 60000
Department Name: Account
Employee Name: Nitish, Employee Department: Account, Employee Salary: 80000
Employee Name: Neeraj, Employee Department: Account, Employee Salary: 40000
ALL Operator
ALL operator is used to return true if all elements within the collection satisfy the given condition. All keyword is used for this purpose. Below are some examples in this category.
Query to find whether all numbers in the array is greater than 10.
class Program
{
static void Main(string[] args)
{
int[] numbers = { 3, 6, 9, 12, 15 };
//Linq Query
var result_Linq = numbers.All(p => p > 10);
//Lambda Expression
var result_Lambda = numbers.All(p => p > 10);
//Display Linq Query Result
Console.WriteLine($"All Numbers greater than 10 (Using Linq): {result_Linq}");
//Display Lambda Expression Result
Console.WriteLine($"All Numbers greater than 10 (Using Lambda): {result_Lambda}");
Console.Read();
}
}
Output
All Numbers greater than 10 (Using Linq): False
All Numbers greater than 10 (Using Lambda): False
Query to find whether all employees in the list of employees having a salary is greater than 50000.
public class Employee
{
public string Emp_Name { get; set; }
public string Emp_Department { get; set; }
public int Emp_Salary { get; set; }
}
class Program
{
static void Main(string[] args)
{
List<Employee> employees = GetEmployeeList();
//Linq Query
var result_Linq = employees.All(p => p.Emp_Salary > 50000);
//Lambda Expression
var result_Lambda = employees.All(p => p.Emp_Salary > 50000);
//Display Linq Query Result
Console.WriteLine($"All Employees with salary greater than 50000 (Using Linq): {result_Linq}");
//Display Lambda Expression Result
Console.WriteLine($"All Employees with salary greater than 50000 (Using Linq): {result_Lambda}");
Console.Read();
}
/// <summary>
/// Method to get employee list
/// </summary>
/// <returns></returns>
private static List<Employee> GetEmployeeList()
{
List<Employee> employees = new List<Employee>();
employees.Add(new Employee() { Emp_Name = "Rahul", Emp_Salary = 70000, Emp_Department = "IT" });
employees.Add(new Employee() { Emp_Name = "Amit", Emp_Salary = 20000, Emp_Department = "HR" });
employees.Add(new Employee() { Emp_Name = "Rajesh", Emp_Salary = 40000, Emp_Department = "HR" });
employees.Add(new Employee() { Emp_Name = "Suresh", Emp_Salary = 60000, Emp_Department = "IT" });
employees.Add(new Employee() { Emp_Name = "Nitish", Emp_Salary = 80000, Emp_Department = "Account" });
employees.Add(new Employee() { Emp_Name = "Neeraj", Emp_Salary = 40000, Emp_Department = "Account" });
return employees;
}
}
Output
All Employees with salary greater than 50000 (Using Linq): False
All Employees with salary greater than 50000 (Using Lambda): False
Query to find grouped a list of employees only for department having all employees whose salary is greater than 50000.
public class Employee
{
public string Emp_Name { get; set; }
public string Emp_Department { get; set; }
public int Emp_Salary { get; set; }
}
class Program
{
static void Main(string[] args)
{
List<Employee> employees = GetEmployeeList();
//Linq Query
var result_Linq = from p in employees group p by p.Emp_Department into g
where g.All(q => q.Emp_Salary > 50000)
select new { Department = g.Key, Employees = g };
//Lambda Expression
var result_Lambda = employees.GroupBy(p => p.Emp_Department).
Where(g => g.All(q => q.Emp_Salary > 50000)).
Select(q => new { Department = q.Key, Employees = q });
//Display Linq Query Result
Console.WriteLine("All Employee with salary greater than 50000 (Using Linq):");
foreach(var item in result_Linq)
{
Console.WriteLine($"Department Name: {item.Department}");
foreach(var employee in item.Employees)
{
Console.WriteLine($"Employee Name: {employee.Emp_Name}, Employee Department: {employee.Emp_Department}, Employee Salary: {employee.Emp_Salary}");
}
}
//Display Lambda Expression Result
Console.WriteLine("All Employee with salary greater than 50000 (Using Lambda):");
foreach (var item in result_Linq)
{
Console.WriteLine($"Department Name: {item.Department}");
foreach (var employee in item.Employees)
{
Console.WriteLine($"Employee Name: {employee.Emp_Name}, Employee Department: {employee.Emp_Department}, Employee Salary: {employee.Emp_Salary}");
}
}
Console.Read();
}
/// <summary>
/// Method to get employee list
/// </summary>
/// <returns></returns>
private static List<Employee> GetEmployeeList()
{
List<Employee> employees = new List<Employee>();
employees.Add(new Employee() { Emp_Name = "Rahul", Emp_Salary = 70000, Emp_Department = "IT" });
employees.Add(new Employee() { Emp_Name = "Amit", Emp_Salary = 20000, Emp_Department = "HR" });
employees.Add(new Employee() { Emp_Name = "Rajesh", Emp_Salary = 40000, Emp_Department = "HR" });
employees.Add(new Employee() { Emp_Name = "Suresh", Emp_Salary = 60000, Emp_Department = "IT" });
employees.Add(new Employee() { Emp_Name = "Nitish", Emp_Salary = 80000, Emp_Department = "Account" });
employees.Add(new Employee() { Emp_Name = "Neeraj", Emp_Salary = 40000, Emp_Department = "Account" });
return employees;
}
}
Output
All Employee with salary greater than 50000 (Using Linq):
Department Name: IT
Employee Name: Rahul, Employee Department: IT, Employee Salary: 70000
Employee Name: Suresh, Employee Department: IT, Employee Salary: 60000
All Employee with salary greater than 50000 (Using Lambda):
Department Name: IT
Employee Name: Rahul, Employee Department: IT, Employee Salary: 70000
Employee Name: Suresh, Employee Department: IT, Employee Salary: 60000