Microsoft LINQ with .NET Framework – An Introduction

By Pritam
In .net
Feb 17th, 2012
0 Comments
1260 Views

Microsoft LINQ with .NET frameworkLINQ, acronym for Language INtegrated Query, is a collection of standard query operators that provides querying facility into .NET framework (both C# and VB). Microsoft shipped LINQ with .NET framework 3.5 for the first time. The power and importance of LINQ can very well be guessed by following example. Suppose you have 10 random numbers stored in a list and you want to arrange them in reverse order. This is how it is usually done 

C# without LINQ
Random rnd = new Random();
List<int> lstRandomNumbers = new List<int>();
for (int i = 0; i < 10; i++) {
     lstRandomNumbers.Add(rnd.Next(100));
 }

 List<int> sortedNumbers = new List<int>( lstRandomNumbers);
 sortedNumbers.Sort();
 sortedNumbers.Reverse();
Code Snippet 1

C# with LINQ

Random rnd = new Random();
 var randomNumbers = from n in Enumerable.Range(1, 10)
                      select rnd.Next(100);
 var sortedNumbers = from random in randomNumbers 
                      orderby random descending
                      select random;
Code Snippet 2 
This is very basic example using LINQ. The task done from step 2-5 in the code snippet 1 is performed in single step (step 2) in code snippet 2. Similarly in code snippet 1 first the list is sorted and then resorted in reverse order to arrange the numbers in decreasing order. The same task is done using LINQ in just one line (line 3 in code snippet 2).So to work with LINQ you should know that any task can be accomplished in just 3 steps:
  • Obtain a data source. 
  • Create Your Query. 
  • Execute the Query. 

The possible data sources can be XML Documents, SQL Databases, ADO.NET Datasets, .NET Collections, and any other format for which a LINQ provider is available.Have a look at the following example explaining the above three steps (Find multiple of three):

// The Three Parts of a LINQ Query:

// 1. Data source.
int[] numbers = new int[10] { 0, 1, 2, 3, 4, 5, 6,7,8,9 };

// 2. Query creation.
// numQuery is an IEnumerable<int>
var numQuery = from num in numbers where (num % 3) == 0 
                select num;

// 3. Query execution.
foreach (int num in numQuery){
  Console.WriteLine("Multiple of three : {0} ", num);
}
Code Snippet 3

 Output: 

3
6
9
It is very important to note here that actual query does not execute unless you iterate over the object return by the query. 

In above example suppose the numbers of array are changed between step 6 and step 8 (like below), then the output would be according to the new array values and not with the old ones. 
for(int i=0;i<10;i++){
  numbers[i] = numbers[i] + 10;
}
Code Snippet 4
Suppose the above three steps were added in between step 6 and step 8 in code snippet 3 then the output would be as follows (the above three lines increase all the values by 10):

Output: 

12
15
18

This is just a brief introduction about LINQ. Soon I will come up with a complete series of tutorials supported by code snippets to explain various queries. Stay connected for the updates.

About the Author

- Co-Founder of IdleBrains, is software Engineer by profession with expertise in .NET technologies and data structures. An avid reader and writer, loves to keep himself well versed with new technologies. When not working can be found on Badminton court or chatting with friends. Among other hobbies, loves to listen old hindi numbers of Kishore Kumar and Mukesh.

Leave a Reply

Your email address will not be published. Required fields are marked *

facebook comments: