Language Integrated Query (LINQ) was introduced in .NET version three.5 to permit a applied scientist to question information from several styles of information sources while not knowing any external language.Querying is the process of obtaining data from a data source. LINQ makes it very easy for you to query data from different kinds of data sources. LINQ is integrated to both C# and VB, and multiple special keywords and syntax for querying using LINQ have been added.

Before the arrival of LINQ, programmers write a different set of codes for querying different data sources. For example, they have to write codes for querying an SQL database using an SQL command or using XPath for querying XML files. With LINQ now in the programmer’s arsenal, querying different data sources requires only the knowledge of the LINQ keywords and methods that were added in .NET 3.5.


Figure 1 – Different Flavors of LINQ

There are multiple flavors of LINQ. This is made possible by LINQ providers  as seen in Figure 1. Visual Studio already includes some of this provider such  as LINQ to Objects. This section of the site will focus on LINQ to Objects which is used to query a collection of objects in your code that implements the IEnumerable<T> interface. Examples of such objects are arrays and lists or a custom collection that you created. There is also an LINQ to SQL which is specifically designed to make it easier to query SQL Server databases. For querying XML files, you can use the LINQ to XML. You can extend LINQ to query more kinds of data sources.  You can create you own providers if you want to support querying another type of  data source using LINQ. The querying techniques that will be thought in the following lessons can be applied on the different flavors of LINQ.

LINQ is made possible by the extension methods that are attached to IEnumerable<T> interface. You can call these methods directly, but you need to have a knowledge of lambda expressions. You can also use query expressions which syntax looks like SQL. Query expressions  are the main tool you will use to query data using LINQ although you can call the extension methods and use lambda expressions.

C# is an important language which suggests that you simply write the step by step codes to create one thing happen, however LINQ promotes declarative programming.This merely implies that you tell laptop|the pc} precisely what you would like and so the computer can handle everything else. Before LINQ, you will be in a position to solely use imperative programming for querying results.For example, suppose that you simply wish to induce all the even numbers from associate array. while not LINQ and exploitation imperative sort of programming, your code can appear as if this:

List<int> evenNumbers = new List<int>();
int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

foreach(int num in numbers)
   if (num % 2 == 0)

Figure 2 – Using Imperative Style of Programming to Query Values

You instruct the computer to go through every value in a collection and then retrieve the number that matches the condition in an ifstatement. Now take a glance at the declarative version that uses the question expression syntax.

int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

var evenNumbers = from n in numbers
                  where n % 2 == 0
                  select n;

Figure 3 – Using a Query Expression to Query Values

Don’t mind the new syntax yet as this will be described in later lessons. You can see that the declarative version is much clear as to what your objective really is.