Select Operator in Linq

by Sachin Singh


Posted on Thursday, 11 February 2021

Tags: Select Operator in Linq

How to think
How to think when working with LINQ

Select Operator

Select is a Projection Operator. Like 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 MaritialStatus {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", MaritialStatus ="UnMarried"},
      new Employee(){Id=1,Name="Nivi",Phone=345678901, Gender="Female", MaritialStatus ="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 MaritialStatus {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", MaritialStatus ="UnMarried"},
    new Employee(){Id=1,Name="Nivi",Phone=345678901, Gender="Female", MaritialStatus ="UnMarried"}
   }
   var contacts = emps.Select(x => new Contact
                          {
                           ContactId = x.Id,
                           Phone = x.Phone,
                           PersonName =(x.Gender == "Female" && x.MaritialStatus == "Married") ? "Mrs." + x.Name : (x.Gender == "Female" && x.MaritialStatus == 
                           "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 operatory to achieve below requirement.
    • Gender="Female" and "MaritialStatus"= "Married" then Add "Mrs." prefix.
    • Gender="Female" and "MaritialStatus" = UnMarried then Add "Ms." prefix else
    • Add "Mr." prefix to the name property's value.

Till now what we discussed is called as Extension Method way or Method Syntax Way of writing Linq queries , all the examples can be rewritten using Sql-Like query syntaxes as shown below

Select Operator in Query Syntax

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 MaritialStatus {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", MaritialStatus ="UnMarried"},
                                                new Employee(){Id=1,Name="Nivi",Phone=345678901, Gender="Female", MaritialStatus ="UnMarried"}
                                             }
  var contacts= (from c in emps
                        select new Contact
                        {
                        ContactId= c.Id,
                        Phone= c.Phone,
                        PersonName= c.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= (from e in emps select new 
               {
                 Id= e.Id,
                 PhoneNumber= e.Phone,
                 Person= e.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= from e in emps select e;

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 MaritialStatus {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", MaritialStatus ="UnMarried"},
   new Employee(){Id=1,Name="Nivi",Phone=345678901, Gender="Female", MaritialStatus ="UnMarried"}
 }
  var contacts= (from e in emps 
                        select new Contact
                       {
                        ContactId = e.Id,
                        Phone = e.Phone,
                        PersonName =( e.Gender == "Female" && e.MaritialStatus == "Married") ? "Mrs." + e.Name : (e.Gender == "Female" && e.MaritialStatus == 
                      "UnMarried") ?"Ms." + e.Name: "Mr"+ e.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 operatory to achieve below requirement.
    • Gender="Female" and "MaritialStatus"= "Married" then Add "Mrs." prefix.
    • Gender="Female" and "MaritialStatus" = UnMarried then Add "Ms." prefix else
    • Add "Mr." prefix to the name property's value.