LINQ
LINQ Stands for Language Integrated Query, so named since it is part of the programming language like C#. It is used to provide consistent access to various data sources like databases and XML. Before LINQ we used various languages and technologies to access different data sources. Now with LINQ we can use the same syntax to access the different data sources from within the language itself.
Without LINQ, ADO.NET is the best choice to manipulate the data of a database. But ADO.NET uses tables, relations and other relational database constructs which needs to be mapped to the application objects. This is more error prone.
Since LINQ is a part of language it benefits from strong typing features like compile time checking for errors. Without LINQ we used to pass SQL to the database at runtime and we get the error only at runtime which is difficult to debug.
There are three main different varieties of LINQ or three LINQ providers provided by Microsoft. All the three providers are built on top of the same LINQ foundation. The providers are used to provide access to different types of data sources. We can also create our own data provider for LINQ to access our data source. This is known as the extensibility feature of the LINQ.
In the above diagram LINQ providers act as a bridge between LINQ and the data sources like SQL and XML.
The three main data providers provided by Microsoft are:
- LINQ to objects for querying in-memory collections
- LINQ to SQL for querying relational databases
- LINQ to XML for querying XML data
LINQ is integrated into C# and VB.NET so that we can query any data source in our favourite programming language. Without LINQ if we wanted to query a database we would use SQL
but with LINQ we can do it in the programming language.
LINQ to Objects for querying in memory collections
The code shown below stores the names in an array then selects the names that starts with A.
string[] names={ "Ashish" ,"Amit","Vivek","Sonali"};
var shortName=from name in names
where name.StartsWith("A")
select name;
foreach(string sname in shortName)
Console.WriteLine("Short Name {0}",sname);
Console.ReadLine();
The above code will print the names Ashish and Amit as they start with A. So you can see this is a more expressive way for selecting the names than if we had to write code without using LINQ.
We can achieve the same result as above by using Query operators which are defined in the System.Linq namespace. These are defined in the enumerable class.
LINQ relies on the deferred query execution for performance which provides Lazy evaluation. This means that the LINQ query is not executed when we assign the query to a variable but afterwards when we actually access the values in the variable one item at a time.
If we want to force the immediate execution of a query thjen we can use the ToList() method when selecting the values.
LINQ relies on the following new language features:
Implicitly typed local variables of expressions |
Which are variables whose types are inferred implicitly from the type |
Object Initializers
|
Which helps in the initialization of objects by assigning properties at creation time |
Lambda Expressions
|
Which are advanced form of anonymous methods with better type inference |