Projection Operators In Linq

by Sachin Singh


Posted on Wednesday, 27 January 2021

Tags: Select in Linq Projection operator in Linq

How to think
How to think when working with LINQ

Projection Operators

A projector machine ,projects slides or film on to a screen , means a projector transforms one form of things into other similarly In LINQ, projection means take all or some properties of an object ,either modify them or don't modify them and project them into a new type ,same type or an anonymous type.

follow below examples for better understanding

Example 1. Projecting object property's into different Object


public class Employee
{
public int Id { get; set;}
public string Name { get; set;}
public int Phone { get; set;}
public string Gender {get;set;}
public string MaritalStatus {get;set;}
}
public class Contact
{
public int ContactId { get; set;}
public string PersonName { get; set;}
public int Phone { get; set;}
}

 List<Employee> emps=new List<Employee>()
 {
 new Employee() {Id=1,Name="Sachin",Phone=345678901,Gender="Male", MaritalStatus ="UnMarried"},
 new Employee(){Id=1,Name="Nivi",Phone=345678901, Gender="Female", MaritalStatus ="UnMarried"}
 }
 var contacts= emps.Select(x=> new Contact
   {
   ContactId=x.Id,
   Phone=x.Phone,
   PersonName=x.Name,
   }).ToList();

In the above example we are projecting Employee's properties to Contact Object which have just three properties ContactId , Phone and PersonName

Example 2.Projecting object property's into anonymous Object


public class Employee
{
public int Id { get; set;}
public string Name { get; set;}
public int Phone { get; set;}
}

List<Employee> emps=new List<Employee>()
{
new Employee() {Id=1,Name="Sachin",Phone=345678901},
new Employee(){Id=1,Name="Sachin",Phone=345678901}
}
var res= emps.Select(x=> new
  {
  Id=x.Id,
  PhoneNumber=x.Phone,
  Person=x.Name,
   }).ToList();

In the above example we are projecting properties of Employee Object to an unnamed (anonymous) object , the anonymous object have three properties Id, PhoneNumber and Person.

Example 3. Projecting object property's into Same Object


 public class Employee
 {
 public int Id { get; set;}
 public string Name { get; set;}
 public int Phone { get; set;}
 }

 List<Employee> emps=new List<Employee>()
 {
 new Employee() {Id=1,Name="Sachin",Phone=345678901},
 new Employee(){Id=1,Name="Sachin",Phone=345678901}
 }
 var employees= emps.Select(x=> new Employee
  {
   Id=x.Id,
   PhoneNumber=x.Phone,
   Person=x.Name,
   }).ToList();

Example 4. Projecting object property's into new Object with modification


 public class Employee
 {
 public int Id { get; set;}
 public string Name { get; set;}
 public int Phone { get; set;}
 public string Gender {get;set;}
 public string MaritalStatus {get;set;}
 }
 public class Contact
 {
 public int ContactId { get; set;}
 public string PersonName { get; set;}
 public int Phone { get; set;}
 }
 List<Employee> emps=new List<Employee>()
 {
 new Employee() {Id=1,Name="Sachin",Phone=345678901,Gender="Male", MaritalStatus ="UnMarried"},
 new Employee(){Id=1,Name="Nivi",Phone=345678901, Gender="Female", MaritalStatus ="UnMarried"}
 }
  var contacts = emps.Select(x => new Contact
  {
  ContactId = x.Id,
  Phone = x.Phone,
  PersonName =(x.Gender == "Female" && x.MaritalStatus == "Married") ? "Mrs." + x.Name : 
 (x.Gender == "Female" && x.MaritalStatus == "UnMarried") ?"Ms." + x.Name: "Mr"+x.Name,
  }).ToList();

Notice in the above example we are computing PersonName property of Contact class object based on the MartialStatus and Gender properties of Employee object , I have used nested ternary operator to achieve below requirement.
    • Gender="Female" and "MaritalStatus"= "Married" then Add "Mrs." prefix.
    • Gender="Female" and "MaritalStatus" = UnMarried then Add "Ms." prefix else
    • Add "Mr." prefix to the name property's value.

Types of LINQ Projection Operators

Linq provides us two flavours of projection operators and obviously each one have its own purpose.
      • Select and
      • Select many

Projection Operators
projection Operators in LINQ

In the coming chapters, we will learn all these Projection operators in detail with examples.