Author : Rahul
Last Modified : 15-Jul-2021
Complexity : Beginner

Projection Operator in LINQ


Projection Operator in LINQ

Projection Operator in LINQ is used to select data from the collection. Just like SQL, SELECT keyword is used to select the data from the collection. 
In other words, Projection Operator transform the result into a new form which is defined by developer. Suppose we have Employee collection with five properties and we want to select only 2 properties, in that case SELECT keyword is used. There are two type of Projection Operator in LINQ:

  1. Select
  2. SelectMany

Select Keyword

If we want to select data from a single collection, then Select Operator is used. Below are some example in this category.

 

Query to produce sequence of ints from int array.

 class Program
 {
   static void Main(string[] args)
   {
     int[] numbers = { 3, 6, 9, 12, 15 };
     
     //Linq Query
     var result_Linq = from p in numbers select p;
     
     //Lambda Expression
     var result_Lambda = numbers.Select(p => p);
     
     //Display Linq Query Result
     Console.WriteLine("Numbers from int array (Using Linq):");
     foreach (int item in result_Linq)
     {
       Console.WriteLine(item);
     }
     
     //Display Lambda Expression Result
     Console.WriteLine("Numbers from int array (Using Lambda):");
     foreach (int item in result_Lambda)
     {
       Console.WriteLine(item);
     }
     
     Console.Read();
   }
 }

Output

Numbers from int array (Using Linq):
3
6
9
12
15
Numbers from int array (Using Lambda):
3
6
9
12
15

 

Query to return name of all employees.

 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_Name;
     
     //Lambda Expression
     var result_Lambda = employees.Select(p => p.Emp_Name);
     
     //Display Linq Query Result
     Console.WriteLine("Employee Names (Using Linq):");
     foreach (string item in result_Linq)
     {
       Console.WriteLine("Employee Name: " + item);
     }
     
     //Display Lambda Expression Result
     Console.WriteLine("Employee Names (Using Lambda):");
     foreach (string item in result_Lambda)
     {
       Console.WriteLine("Employee Name: " + 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

Employee Names (Using Linq):
Employee Name: Rahul
Employee Name: Amit
Employee Name: Suresh
Employee Name: Nitish
Employee Names (Using Lambda):
Employee Name: Rahul
Employee Name: Amit
Employee Name: Suresh
Employee Name: Nitish

 

Query to return sequence of the uppercase and lowercase versions of each word in the array.

 class Program
 {
   static void Main(string[] args)
   {
     string[] words = { "rAhuL", "KuMar", "GoeL" };
     
     //Linq Query
     var result_Linq = from p in words select new { UpperCase = p.ToUpper(), LowerCase = p.ToLower() };
     
     //Lambda Expression
     var result_Lambda = words.Select(p => new { UpperCase = p.ToUpper(), LowerCase = p.ToLower() });
     
     //Display Linq Query Result
     Console.WriteLine("UpperCase and LowerCase version of the Words (Using Linq):");
     foreach (var item in result_Linq)
     {
       Console.WriteLine("UpperCase:" + item.UpperCase + ", LowerCase:" + item.LowerCase);
     }
     
     //Display Lambda Expression Result
     Console.WriteLine("UpperCase and LowerCase version of the Words (Using Lambda):");
     foreach (var item in result_Lambda)
     {
       Console.WriteLine("UpperCase:" + item.UpperCase + ", LowerCase:" + item.LowerCase);
     }
     
     Console.Read();
   }
 }

Output

UpperCase and LowerCase version of the Words (Using Linq):
UpperCase:RAHUL, LowerCase:rahul
UpperCase:KUMAR, LowerCase:kumar
UpperCase:GOEL, LowerCase:goel
UpperCase and LowerCase version of the Words (Using Lambda):
UpperCase:RAHUL, LowerCase:rahul
UpperCase:KUMAR, LowerCase:kumar
UpperCase:GOEL, LowerCase:goel

 

Query to return list of employees with Employee Name and Salary.

 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 new { EmployeeName = p.Emp_Name, EmployeeSalary = p.Emp_Salary };
     
     //Lambda Expression
     var result_Lambda = employees.Select(p => new { EmployeeName = p.Emp_Name, EmployeeSalary = p.Emp_Salary });
     
     //Display Linq Query Result
     Console.WriteLine("Employee list with Employee Name and its Salary (Using Linq):");
     foreach (var item in result_Linq)
     {
       Console.WriteLine("Employee Name: " + item.EmployeeName + ", Employee Salary: " + item.EmployeeSalary);
     }
     
     //Display Lambda Expression Result
     Console.WriteLine("Employee list with Employee Name and its Salary (Using Lambda):");
     foreach (var item in result_Lambda)
     {
       Console.WriteLine("Employee Name: " + item.EmployeeName + ", Employee Salary: " + item.EmployeeSalary);
     }
     
     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

Employee list with Employee Name and its Salary (Using Linq):
Employee Name: Rahul, Employee Salary: 70000
Employee Name: Amit, Employee Salary: 20000
Employee Name: Suresh, Employee Salary: 40000
Employee Name: Nitish, Employee Salary: 80000
Employee list with Employee Name and its Salary (Using Lambda):
Employee Name: Rahul, Employee Salary: 70000
Employee Name: Amit, Employee Salary: 20000
Employee Name: Suresh, Employee Salary: 40000
Employee Name: Nitish, Employee Salary: 80000

 

Query to return list of employees with Employee Name and Salary having IT department.

 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_Department == "IT" select new { EmployeeName = p.Emp_Name, EmployeeSalary = p.Emp_Salary };
     
     //Lambda Expression
     var result_Lambda = employees.Where(p => p.Emp_Department == "IT").Select(p => new { EmployeeName = p.Emp_Name, EmployeeSalary = p.Emp_Salary });
     
     //Display Linq Query Result
     Console.WriteLine("Employee list with Employee Name and its Salary having IT department (Using Linq):");
     foreach (var item in result_Linq)
     {
       Console.WriteLine("Employee Name: " + item.EmployeeName + ", Employee Salary: " + item.EmployeeSalary);
     }
     
     //Display Lambda Expression Result
     Console.WriteLine("Employee list with Employee Name and its Salary having IT department (Using Lambda):");
     foreach (var item in result_Lambda)
     {
       Console.WriteLine("Employee Name: " + item.EmployeeName + ", Employee Salary: " + item.EmployeeSalary);
     }
     
     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

Employee list with Employee Name and its Salary having IT department (Using Linq):
Employee Name: Rahul, Employee Salary: 70000
Employee Name: Suresh, Employee Salary: 40000
Employee list with Employee Name and its Salary having IT department (Using Lambda):
Employee Name: Rahul, Employee Salary: 70000
Employee Name: Suresh, Employee Salary: 40000

 

SelectMany Keyword

If we want to select data from multiple collection, SelectMany operator is used. Suppose the data is present in different collections and we want to display the data as a single collection, SelectMany operator can be used. Below are some example in this category.


Query to returns all pairs of numbers from two arrays such that the number from firstArray is less than the number from secondArray.

 class Program
 {
   static void Main(string[] args)
   {
     int[] firstArray = { 3, 6, 9, 12, 15 };
     int[] secondArray = { 1, 4, 9, 12 };
     
     //Linq Query
     var result_Linq = from p in firstArray from q in secondArray where p < q select new { p, q };
     
     //Lambda Expression
     var result_Lambda = firstArray.SelectMany(p => secondArray, (p, q) => new { p, q }).Where(@t => t.p < t.q);
     
     //Display Linq Query Result
     Console.WriteLine("Numbers with first array is less than second array (Using Linq):");
     foreach (var item in result_Linq)
     {
       Console.WriteLine($"{item.p} is less than {item.q}");
     }
     
     //Display Lambda Expression Result
     Console.WriteLine("Numbers with first array is less than second array (Using Lambda):");
     foreach (var item in result_Lambda)
     {
       Console.WriteLine($"{item.p} is less than {item.q}");
     }
     
     Console.Read();
   }
 }

Output

Numbers with first array is less than second array (Using Linq):
3 is less than 4
3 is less than 9
3 is less than 12
6 is less than 9
6 is less than 12
9 is less than 12
Numbers with first array is less than second array (Using Lambda):
3 is less than 4
3 is less than 9
3 is less than 12
6 is less than 9
6 is less than 12
9 is less than 12

 

Query to return list of employees with Employee Name and Department.

 public class Employee
 {
   public string Emp_Name { get; set; }
   public int Emp_Department_Id { get; set; }
   public int Emp_Salary { get; set; }
 }
 
 public class Department
 {
   public int Department_Id { get; set; }
   public string Department_Name { get; set; }
 }
 
 class Program
 {
   static void Main(string[] args)
   {
     List<Employee> employees = GetEmployeeList();
     List<Department> departments = GetDepartmentList();
     
     //Linq Query
     var result_Linq = from p in employees from q in departments where p.Emp_Department_Id == q.Department_Id select new { EmployeeName = p.Emp_Name, DepartmentName = q.Department_Name};
     
     //Lambda Expression
     var result_Lambda = employees.SelectMany(p => departments, (p, q) => new { p, q }).
       Where(@t => t.p.Emp_Department_Id == t.q.Department_Id).
       Select(@t => new { EmployeeName = t.p.Emp_Name, DepartmentName = t.q.Department_Name });
       
     //Display Linq Query Result
     Console.WriteLine("Employee Details (Using Linq):");
     foreach (var item in result_Linq)
     {
       Console.WriteLine($"Employee Name: {item.EmployeeName} , Employee Department: {item.DepartmentName}");
     }
     
     //Display Lambda Expression Result
     Console.WriteLine("Employee Details (Using Lambda):");
     foreach (var item in result_Lambda)
     {
       Console.WriteLine($"Employee Name: {item.EmployeeName} , Employee Department: {item.DepartmentName}");
     }
     
     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_Id = 1 });
     employees.Add(new Employee() { Emp_Name = "Amit", Emp_Salary = 20000, Emp_Department_Id = 2 });
     employees.Add(new Employee() { Emp_Name = "Suresh", Emp_Salary = 40000, Emp_Department_Id = 1 });
     employees.Add(new Employee() { Emp_Name = "Nitish", Emp_Salary = 80000, Emp_Department_Id = 3 });
     return employees;
   }
   
   /// <summary>
   /// Method to get department list
   /// </summary>
   /// <returns></returns>
   private static List<Department> GetDepartmentList()
   {
     List<Department> departments = new List<Department>();
     departments.Add(new Department() { Department_Id = 1, Department_Name = "IT" });
     departments.Add(new Department() { Department_Id = 2, Department_Name = "HR" });
     departments.Add(new Department() { Department_Id = 3, Department_Name = "Account" });
     return departments;
   }
 }

Output

Employee Details (Using Linq):
Employee Name: Rahul , Employee Department: IT
Employee Name: Amit , Employee Department: HR
Employee Name: Suresh , Employee Department: IT
Employee Name: Nitish , Employee Department: Account
Employee Details (Using Lambda):
Employee Name: Rahul , Employee Department: IT
Employee Name: Amit , Employee Department: HR
Employee Name: Suresh , Employee Department: IT
Employee Name: Nitish , Employee Department: Account