Set Operator in LINQ
Set Operator in LINQ takes data from the different collection or same collection and returns the sequence based on requirements like combining elements, removing duplicates, etc. There are four Set Operator are available in LINQ as follow:
- UNION
- INTERSECT
- DISTINCT
- EXCEPT
Now we will discuss each operator one by one.
UNION Operator
UNION operator is used to take data from different collections and return a sequence of unique elements. Union keyword is used for this purpose. Below are some examples in this category.
Query to create one sequence that contains unique values from two arrays.
class Program
{
static void Main(string[] args)
{
int[] numberA = { 3, 6, 9, 12, 15 };
int[] numberB = { 2, 4, 6, 8, 12 };
//Linq Query
var result_Linq = numberA.Union(numberB);
//Lambda Expression
var result_Lambda = numberA.Union(numberB);
//Display Linq Query Result
Console.WriteLine("Unique Numbers (Using Linq):");
foreach (int item in result_Linq)
{
Console.WriteLine(item);
}
//Display Lambda Expression Result
Console.WriteLine("Unique Numbers (Using Lambda):");
foreach (int item in result_Lambda)
{
Console.WriteLine(item);
}
Console.Read();
}
}
Output
Unique Numbers (Using Linq):
3
6
9
12
15
2
4
8
Unique Numbers (Using Lambda):
3
6
9
12
15
2
4
8
Query to create one sequence that contains unique names from employee names and manager names.
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();
List<Employee> managers = GetManagerList();
//Linq Query
var employee_Name = from p in employees select p.Emp_Name;
var employee_Dept = from p in managers select p.Emp_Name;
var result_Linq = employee_Name.Union(employee_Dept);
//Lambda Expression
var employee_Name_L = employees.Select(p => p.Emp_Name);
var employee_Dept_L = managers.Select(p => p.Emp_Name);
var result_Lambda = employee_Name_L.Union(employee_Dept_L);
//Display Linq Query Result
Console.WriteLine("Unique Names between Employee Name and Manager Name (Using Linq):");
foreach (string item in result_Linq)
{
Console.WriteLine(item);
}
//Display Lambda Expression Result
Console.WriteLine("Unique Names between Employee Name and Manager Name (Using Lambda):");
foreach (string item in result_Lambda)
{
Console.WriteLine(item);
}
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 = "Suresh", Emp_Salary = 40000, Emp_Department = "IT" });
employees.Add(new Employee() { Emp_Name = "Nitish", Emp_Salary = 80000, Emp_Department = "Account" });
return employees;
}
/// <summary>
/// Method to get manager list
/// </summary>
/// <returns></returns>
private static List<Employee> GetManagerList()
{
List<Employee> managers = new List<Employee>();
managers.Add(new Employee() { Emp_Name = "Rahul", Emp_Salary = 170000, Emp_Department = "IT" });
managers.Add(new Employee() { Emp_Name = "Amit", Emp_Salary = 120000, Emp_Department = "HR" });
managers.Add(new Employee() { Emp_Name = "Sandeep", Emp_Salary = 140000, Emp_Department = "IT" });
managers.Add(new Employee() { Emp_Name = "Neeraj", Emp_Salary = 180000, Emp_Department = "Account" });
return managers;
}
}
Output
Unique Names between Employee Name and Manager Name (Using Linq):
Rahul
Amit
Suresh
Nitish
Sandeep
Neeraj
Unique Names between Employee Name and Manager Name (Using Lambda):
Rahul
Amit
Suresh
Nitish
Sandeep
Neeraj
INTERSECT Operator
INTERSECT Operator is used to take data from different collections and return a sequence of common elements. Intersect keyword is used for this purpose. Below are some examples in this category.
Query to create one sequence that contains common values from two arrays.
class Program
{
static void Main(string[] args)
{
int[] numberA = { 3, 6, 9, 12, 15 };
int[] numberB = { 2, 4, 6, 8, 12 };
//Linq Query
var result_Linq = numberA.Intersect(numberB);
//Lambda Expression
var result_Lambda = numberA.Intersect(numberB);
//Display Linq Query Result
Console.WriteLine("Common Numbers (Using Linq):");
foreach (int item in result_Linq)
{
Console.WriteLine(item);
}
//Display Lambda Expression Result
Console.WriteLine("Common Numbers (Using Lambda):");
foreach (int item in result_Lambda)
{
Console.WriteLine(item);
}
Console.Read();
}
}
Output
Common Numbers (Using Linq):
6
12
Common Numbers (Using Lambda):
6
12
Query to create one sequence that contains common names from employee names and manager names.
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();
List<Employee> managers = GetManagerList();
//Linq Query
var employee_Name = from p in employees select p.Emp_Name;
var employee_Dept = from p in managers select p.Emp_Name;
var result_Linq = employee_Name.Intersect(employee_Dept);
//Lambda Expression
var employee_Name_L = employees.Select(p => p.Emp_Name);
var employee_Dept_L = managers.Select(p => p.Emp_Name);
var result_Lambda = employee_Name_L.Intersect(employee_Dept_L);
//Display Linq Query Result
Console.WriteLine("Common Names between Employee Name and Manager Name (Using Linq):");
foreach (string item in result_Linq)
{
Console.WriteLine(item);
}
//Display Lambda Expression Result
Console.WriteLine("Common Names between Employee Name and Manager Name (Using Lambda):");
foreach (string item in result_Lambda)
{
Console.WriteLine(item);
}
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 = "Suresh", Emp_Salary = 40000, Emp_Department = "IT" });
employees.Add(new Employee() { Emp_Name = "Nitish", Emp_Salary = 80000, Emp_Department = "Account" });
return employees;
}
/// <summary>
/// Method to get manager list
/// </summary>
/// <returns></returns>
private static List<Employee> GetManagerList()
{
List<Employee> managers = new List<Employee>();
managers.Add(new Employee() { Emp_Name = "Rahul", Emp_Salary = 170000, Emp_Department = "IT" });
managers.Add(new Employee() { Emp_Name = "Amit", Emp_Salary = 120000, Emp_Department = "HR" });
managers.Add(new Employee() { Emp_Name = "Sandeep", Emp_Salary = 140000, Emp_Department = "IT" });
managers.Add(new Employee() { Emp_Name = "Neeraj", Emp_Salary = 180000, Emp_Department = "Account" });
return managers;
}
}
Output
Common Names between Employee Name and Manager Name (Using Linq):
Rahul
Amit
Common Names between Employee Name and Manager Name (Using Lambda):
Rahul
Amit
DISTINCT Operator
DISTINCT Operator is used to take data from a collection and return a sequence of unique elements. Distinct keyword is used for this purpose. Below are some examples in this category.
Query to remove duplicate elements from the number array.
class Program
{
static void Main(string[] args)
{
int[] numbers = { 3, 6, 6, 12, 12 };
//Linq Query
var result_Linq = (from p in numbers select p).Distinct();
//Lambda Expression
var result_Lambda = numbers.Distinct();
//Display Linq Query Result
Console.WriteLine("Distinct Numbers (Using Linq):");
foreach (int item in result_Linq)
{
Console.WriteLine(item);
}
//Display Lambda Expression Result
Console.WriteLine("Distinct Numbers (Using Lambda):");
foreach (int item in result_Lambda)
{
Console.WriteLine(item);
}
Console.Read();
}
}
Output
Distinct Numbers (Using Linq):
3
6
12
Distinct Numbers (Using Lambda):
3
6
12
Query to find unique department names from the employee list.
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 select p.Emp_Department).Distinct();
//Lambda Expression
var result_Lambda = employees.Select(p=>p.Emp_Department).Distinct();
//Display Linq Query Result
Console.WriteLine("Distinct Employee Department (Using Linq):");
foreach (string item in result_Linq)
{
Console.WriteLine(item);
}
//Display Lambda Expression Result
Console.WriteLine("Distinct Employee Department (Using Lambda):");
foreach (string item in result_Lambda)
{
Console.WriteLine(item);
}
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 = "Suresh", Emp_Salary = 40000, Emp_Department = "IT" });
employees.Add(new Employee() { Emp_Name = "Nitish", Emp_Salary = 80000, Emp_Department = "Account" });
return employees;
}
}
Output
Distinct Employee Department (Using Linq):
IT
HR
Account
Distinct Employee Department (Using Lambda):
IT
HR
Account
EXCEPT Operator
EXCEPT Operator takes data from different collections and returns a sequence of elements from the first collection, which is not present in the second collection. Except keyword is used for this purpose. Below are some examples in this category.
Query to create one sequence that contains values from the first array but not in the second array.
class Program
{
static void Main(string[] args)
{
int[] numberA = { 3, 6, 9, 12, 15 };
int[] numberB = { 2, 4, 6, 8, 12 };
//Linq Query
var result_Linq = numberA.Except(numberB);
//Lambda Expression
var result_Lambda = numberA.Except(numberB);
//Display Linq Query Result
Console.WriteLine("Numbers not in second array (Using Linq):");
foreach (int item in result_Linq)
{
Console.WriteLine(item);
}
//Display Lambda Expression Result
Console.WriteLine("Numbers not in second array (Using Lambda):");
foreach (int item in result_Lambda)
{
Console.WriteLine(item);
}
Console.Read();
}
}
Output
Numbers not in second array (Using Linq):
3
9
15
Numbers not in second array (Using Lambda):
3
9
15
Query to create one sequence that contains names from employee names, not manager names.
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();
List<Employee> managers = GetManagerList();
//Linq Query
var employee_Name = from p in employees select p.Emp_Name;
var employee_Dept = from p in managers select p.Emp_Name;
var result_Linq = employee_Name.Except(employee_Dept);
//Lambda Expression
var employee_Name_L = employees.Select(p => p.Emp_Name);
var employee_Dept_L = managers.Select(p => p.Emp_Name);
var result_Lambda = employee_Name_L.Except(employee_Dept_L);
//Display Linq Query Result
Console.WriteLine("Employee Names which are not in Manager Name List (Using Linq):");
foreach (string item in result_Linq)
{
Console.WriteLine(item);
}
//Display Lambda Expression Result
Console.WriteLine("Employee Names which are not in Manager Name List (Using Lambda):");
foreach (string item in result_Lambda)
{
Console.WriteLine(item);
}
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 = "Suresh", Emp_Salary = 40000, Emp_Department = "IT" });
employees.Add(new Employee() { Emp_Name = "Nitish", Emp_Salary = 80000, Emp_Department = "Account" });
return employees;
}
/// <summary>
/// Method to get manager list
/// </summary>
/// <returns></returns>
private static List<Employee> GetManagerList()
{
List<Employee> managers = new List<Employee>();
managers.Add(new Employee() { Emp_Name = "Rahul", Emp_Salary = 170000, Emp_Department = "IT" });
managers.Add(new Employee() { Emp_Name = "Amit", Emp_Salary = 120000, Emp_Department = "HR" });
managers.Add(new Employee() { Emp_Name = "Sandeep", Emp_Salary = 140000, Emp_Department = "IT" });
managers.Add(new Employee() { Emp_Name = "Neeraj", Emp_Salary = 180000, Emp_Department = "Account" });
return managers;
}
}
Output
Employee Names which are not in Manager Name List (Using Linq):
Suresh
Nitish
Employee Names which are not in Manager Name List (Using Lambda):
Suresh
Nitish