Introduction To LINQ

In this article you will learn about an introduction of LINQ and styles of writing LINQ queries.

LINQ

"LINQ (Language Integrated Query) is a Microsoft programming methodology that adds query capabilities into .NET based programming languagesIt offers comprehensive syntax for manipulating data."

Why LINQ
  1. Reduce the amount of code.
  2. A better understanding of the intent of what the code is doing .
  3. A similar set of LINQ queries can be applied on different data sources.
  4. LINQ enables queries as first class citizen in C# and Visual Studio.
The building blocks of LINQ
 
There are two fundamental blocks in LINQ
  1. Sequence
  2. Element
A sequence is an instance of class that implements IEnumerable<T> interface. A sequence is basically a list of items and each item is treated as an element.
There are two types of sequences.
  1. Local
  2. Remote
  • Local Sequence implements IEnumerable <T> interface.
  • Remote Sequence implements IQueryable<T> interface.
An example of Local Sequence
  1. class Program  
  2. {  
  3.     static void Main(string[] args)  
  4.   {  
  5.         int[] list =  
  6.           {  
  7.             1,  
  8.             2,  
  9.             3,  
  10.             5,  
  11.             6,  
  12.             7,  
  13.             8  
  14.         };  
  15.         foreach(var item in list)  
  16.         Console.WriteLine(item);  
  17.         Console.ReadKey();  
  18.     }  
  19. }  
Output

It would simply display the numbers in the list. Now we will see how we can manipulate this data with the LINQ. 
 
Example: Display those numbers which are greater than two.

Code
  1. class Program  
  2. {  
  3.     static void Main(string[] args)  
  4.   {  
  5.         int[] list =  
  6.           {  
  7.             1,  
  8.             2,  
  9.             4,  
  10.             5,  
  11.             6,  
  12.             7,  
  13.             8  
  14.         };  
  15.         var result = list.Where(x => x > 2);  
  16.         foreach(var item in result)  
  17.         Console.WriteLine(item);  
  18.         Console.ReadKey();  
  19.     }  
  20. }  
Output
 


You can see that how we can easily manipulate data and can get resuls according to our requirements. 
 
2. Deferred Execution

It means that the execution of LINQ query does not execute at the time of creation. Rather it is executed when it is used. Actually the execution of the LINQ query starts with the foreach loop.
 
Example:

Code:
  1. class Program  
  2. {  
  3.     static void Main(string[] args)  
  4.   {  
  5.         int[] list =   
  6.           {  
  7.             1,  
  8.             2,  
  9.             3,  
  10.             5,  
  11.             6,  
  12.             7,  
  13.             8  
  14.         };  
  15.         var result = list.Where(x => x > 2);  
  16.         list[0] = 99; //Change the first element of the list  
  17.   
  18.         foreach(var item in result) {  
  19.             Console.WriteLine(item);  
  20.         }  
  21.         Console.ReadKey();  
  22.     }  
  23. }  
Output
 
















 
 
Notice that result 99 has been included even at that time the query was constructed. There are number of operators that run at the time of query creation such as ToArray, ToDictionery, etc.

Example:

Code:
  1. class Program   
  2. {  
  3.     static void Main(string[] args)  
  4.   {  
  5.         int[] list =  
  6.         {  
  7.             1,  
  8.             2,  
  9.             4,  
  10.             5,  
  11.             6,  
  12.             7,  
  13.             8  
  14.         };  
  15.         var result = list.Where(x => x > 2).ToArray();  
  16.         list[0] = 99; //Change the first element of the list  
  17.         foreach(var item in result) {  
  18.             Console.WriteLine(item);  
  19.         }  
  20.         Console.ReadKey();  
  21.     }  
  22. }  
Output
















 
 
Notice that 99 is not included because of additional ToArray(). 
 
Styles of writing LINQ Query

There are two styles of writing LINQ queries.
  1. Fluent Style
  2. Query Expression Style
Query Expression Style is mostly used because its syntax makes more sense.

An example of Fluent Style

Code
  1. class Employee  
  2. {  
  3.     public string Name  
  4.   {  
  5.         set;  
  6.         get;  
  7.     }  
  8.     public int Salary  
  9.     {  
  10.         set;  
  11.         get;  
  12.     }  
  13.     static void Main(string[] args)   
  14.     {  
  15.         Employee[] employee =   
  16.           {  
  17.             new Employee   
  18.           {  
  19.                 Name = "Usman", Salary = 50000  
  20.             },  
  21.             new Employee  
  22.         {  
  23.                 Name = "Ayaz", Salary = 60000  
  24.             },  
  25.             new Employee  
  26.             {  
  27.                 Name = "Ahmad", Salary = 70000  
  28.             }  
  29.         };  
  30.         var emploee_List = employee.Where(x => x.Salary > 50000) //select on the basis of salaries  
  31.             .OrderBy(x => x.Name) //arrange on the basis of Name as Name is string so it will order on the basis of alphabet  
  32.             .Select(x => x.Name); //select on name  
  33.   
  34.         foreach(var emp in emploee_List)  
  35.         {  
  36.             Console.WriteLine(emp);  
  37.         }  
  38.         Console.ReadKey();  
  39.     }  
  40. }  
Output


An example of Query Expression Style

Code:

  1. class Employee  
  2.   
  3. {  
  4.   
  5.     public string Name  
  6.   {  
  7.         set;  
  8.         get;  
  9.     }  
  10.   
  11.     public int Salary  
  12.     {  
  13.         set;  
  14.         
  15.         get;  
  16.     }  
  17.   
  18.     static void Main(string[] args)  
  19.   
  20.     {  
  21.   
  22.         Employee[] employee =   
  23.           {  
  24.   
  25.             new Employee   
  26.           {  
  27.                 Name = "Usman", Salary = 50000  
  28.             },  
  29.   
  30.             new Employee  
  31.         {  
  32.                 Name = "Ayaz", Salary = 60000  
  33.             },  
  34.   
  35.             new Employee  
  36.             {  
  37.                 Name = "Ahmad", Salary = 70000  
  38.             }  
  39.   
  40.         };  
  41.   
  42.         var emploee_List =  
  43.   
  44.             from i in employee where i.Salary > 50000 //select those employees whose salary is greater than 50000  
  45.   
  46.         orderby i.Name  
  47.   
  48.         select i.Name;  
  49.   
  50.         foreach(var emp in emploee_List)  
  51.   
  52.         {  
  53.   
  54.             Console.WriteLine(emp);  
  55.   
  56.         }  
  57.   
  58.         Console.ReadKey();  
  59.   
  60.     }  
  61.   
  62. }  

Output


You can see that Expression Query Styles makes more sense than Fluent Query Style. In the next article, I will write about operators and joins in LINQ.