Various Ways To Write LINQ Queries

In my previous article we learned what LINQ is.

If you missed it by chance then click here.

There are the following two ways to write LINQ queries using the Standard Query operators, in other words Select, From, Where, Orderby, Join, Groupby and many more.

  1. Using lambda expressions.
  2. Using SQL like query expressions.

The standard query operators are implemented as extension methods on an IEnumerable<T> interface. So, all the generic and non-generic classes implement IEnumerable<T>. That means on all these types we can use the LINQ query operators.

Demo

In my console application, I have a class Employee with three auto-implemented properties Id, Name and gender.

The Employee class also has a GetEmployees method that returns a List<Employee>. The list class is in the System.Collection.Generic namespace and we know all the classes present in this namespace implements IEnumerable<T>. That means we can use the LINQ query operators in the List.

  1. using System.Collections.Generic;  
  2.   
  3. namespace WritingLinqQueries {  
  4.     class Employees {  
  5.         //three auto-implemented properties  
  6.         public int Id { getset; }  
  7.         public string Name { getset; }  
  8.         public string Gender { getset; }  
  9.   
  10.         //a static method which returns a List<Employees> back  
  11.         public static List<Employees> GetEmployees() {  
  12.             //create a new List<Employee> object  
  13.             List<Employees> employeeList = new List<Employees>();  
  14.   
  15.             //create and five different instance of employees and assign values for the properties  
  16.             Employees employeeOne = new Employees {  
  17.                 Id = 1, Name = "Sara", Gender = "Female"  
  18.             };  
  19.             //add the employee object in the employeeList  
  20.             employeeList.Add(employeeOne);  
  21.   
  22.             Employees employeeTwo = new Employees {  
  23.                 Id = 2, Name = "James", Gender = "Male"  
  24.             };  
  25.             employeeList.Add(employeeTwo);  
  26.   
  27.             Employees employeeThree = new Employees {  
  28.                 Id = 3, Name = "Lara Croft", Gender = "Female"  
  29.             };  
  30.             employeeList.Add(employeeThree);  
  31.   
  32.             Employees employeeFour = new Employees {  
  33.                 Id = 4, Name = "Max", Gender = "Male"  
  34.             };  
  35.             employeeList.Add(employeeFour);  
  36.   
  37.             Employees employeeFive = new Employees {  
  38.                 Id = 5, Name = "Aiden", Gender = "Male"  
  39.             };  
  40.             employeeList.Add(employeeFive);  
  41.   
  42.             //after adding all the employees, return the employeeList back  
  43.             return employeeList;  
  44.         }  
  45.     }  

Using Lambda Expression

In the main program we need to call the employees GetEmployees method and for that we can write the following in our main method:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4.   
  5. namespace WritingLinqQueries {  
  6.     class Program {  
  7.         static void Main() {  
  8.             //using lambda expression  
  9.             IEnumerable<Employees> employee = Employees.GetEmployees().Where(emp => emp.Gender == "Female");//Where method returns IEnumerable<Employees> back  
  10.   
  11.             //loop thru each Employee e in employee object  
  12.             foreach (Employees e in employee) {  
  13.                 Console.WriteLine(e.Name);  
  14.             }  
  15.         }  
  16.     }  

console

The other way to do it is by using the SQL like query.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4.   
  5. namespace WritingLinqQueries {  
  6.     class Program {  
  7.         static void Main() {  
  8.             //using SQL like query  
  9.             IEnumerable<Employees> employee = from emp in Employees.GetEmployees() where emp.Gender == "Male" select emp;  
  10.             //loop thru each Employee e in employee object  
  11.             foreach (Employees e in employee) {  
  12.                 Console.WriteLine(e.Name);  
  13.             }  
  14.         }  
  15.     }  

output

Note

From a performance perspective there is no difference between the two because behind the scene, LINQ queries written using SQL like query expressions are translated into their lambda expressions before they are compiled.

The next article of this series explains extension methods.

Until then keep learning.
Thank you,


Similar Articles