LINQ (Language Integrated Query)
LINQ is known as Language Integrated Query and it is introduced in .NET 3.5 and Visual Studio 2008. The beauty of LINQ is it provides the ability to .NET languages(like C#, VB.NET, etc.) to generate queries to retrieve data from the data source. For example, a program may get information from the student records or accessing employee records, etc. In, past years, such type of data is stored in a separate database from the application, and you need to learn different types of query language to access such type of data like SQL, XML, etc. And also you cannot create a query using C# language or any other .NET language.
To overcome such type of problems Microsoft developed LINQ. It attaches one, more power to the C# or .NET languages to generate a query for any LINQ compatible data source. And the best part is the syntax used to create a query is the same no matter which type of data source is used means the syntax of creating a query data in a relational database is same as that used to create query data stored in an array there is no need to use SQL or any other non-.NET language mechanism. You can also use LINQ with SQL, with XML files, with ADO.NET, with web services, and with any other database.
In C#, LINQ is present in System.Linq namespace. It provides different type of classes and methods which supports LINQ queries. In this namespace:
- Enumerable class holds standard query operators that operate on object which executes IEnumerable<T>.
- Queryable class holds standard query operators that operate on object which executes IQueryable<T>.
Architecture of LINQ
The architecture of LINQ is 3-layered architecture. In which the topmost layer contains language extension and the bottom layer contains data sources that generally object implementing IEnumerable <T> or IQueryable <T> generic interfaces. The architecture of the LINQ is as shown in the below image:
Why we use LINQ?
Now we learn why LINQ is created, or why we use LINQ. The following points explain why we use LINQ.
- The main purpose behind creating LINQ is, before C# 3.0 we use for loop, foreach loop, or delegates traverse a collection to find a specific object, but the disadvantage of using these methods for finding an object is you need to write a large sum of code to find an object which is more time-consuming and make your program less readable. So to overcome these problems LINQ introduced. Which perform the same operation in a few numbers of lines and make your code more readable and also you can use the same code in other programs.
- It also provides full type checking at compile time, it helps us to detect the error at the runtime, so that we can easily remove them.
- LINQ is it is simple, well-ordered, and high-level language than SQL
- You can also use LINQ with C# array and collections. It gives you a new direction to solve the old problems in an effective manner.
- With the help of LINQ you can easily work with any type of data source like XML, SQL, Entities, objects, etc. A single query can work with any type of database no need to learn different types of languages.
- LINQ supports query expression, Implicitly typed variables, Object and collection initializers, Anonymous types, Extension methods, and Lambda expressions.
Advantages of LINQ
- User does not need to learn new query languages for a different type of data source or data format.
- It increase the readability of the code.
- Query can be reused.
- It gives type checking of the object at compile time.
- It provides IntelliSense for generic collections.
- It can be used with array or collections.
- LINQ supports filtering, sorting, ordering, grouping.
- It makes easy debugging because it is integrated with C# language.
- It provides easy transformation means you can easily convert one data type into another data type like transforming SQL data into XML data.
Please Login to comment...