In this lesson, we are going to discuss about LINQ Architecture. The term LINQ stands for Language Integrated Query and it is pronounced as LINK. LINQ enables us to query any type of data store (SQL Server, XML documents, Objects in memory etc). Nowadays the use of use LINQ increasing rapidly. So, as a developer, we should understand the Linq and its architecture. Please find the step by step lesson of linq tutorials on this link.
What is LINQ?
The LINQ (Language Integrated Query) is a part of a language but not a complete language. It was introduced by Microsoft with .NET Framework 3.5 and C# 3.0 and is available in System.Linq namespace.
LINQ provides us common query syntax which allows us to query the data from various data sources. That means using a single query we can get or set the data from various data sources such as SQL Server database, XML documents, ADO.NET Datasets, and any other in-memory objects such as Collections, Generics, etc
Why should we learn LINQ?
LINQ enables us to work with these different data sources using a similar coding style without having the need to know the syntax specific to the data source. Suppose we are developing a .NET Application and that application requires data from different data sources. For example
- The application needs data from SQL Server Database. So as a developer in order to access the data from SQL Server Database, we need to understand ADO.NET and SQL Server-specific syntaxes. If the database is Oracle then we need to learn SQL Syntax specific to Oracle Database.
- The application also needs data from an XML Document. So as a developer in order to work with XML Document, we need to understand XPath and XSLT queries.
- The application also needs to manipulate the data (objects) in memory such as List<customers>, List<orders>, etc. So as a developer we should also have to understand how to work with in-memory objects.
- Another benefit of using LINQ is that it provides intelligence and compile time error checking.
LINQ provides a uniform programming model (i.e. common query syntax) which allows us to work with different data sources but using a standard or you can say unified coding style. As a result, we don’t require to learn different syntaxes to query different data sources.
LINQ Architecture & LINQ Providers
According to the below diagram, we can write the LINQ queries using any .NET supported programming languages such as C#, VB.NET, J#, etc.
The LINQ provider is a software component that lies between the LINQ queries and the actual data source. The LINQ provider will convert the LINQ queries into a format that can be understood by the underlying data source. For example, LINQ to SQL provider will convert the LINQ queries to SQL statements which can be understood by the SQL Server database. Similarly, the LINQ to XML provider will convert the queries into a format that can be understood by the XML document.
LINQ Providers?
A LINQ provider is software that implements the IQueryProvider and IQueryable interface for a particular data source. In other words, it allows us to write LINQ queries against that data source. If you want to create your custom LINQ provider then it must implement IQueryProvider and IQueryable interface. Without LINQ Provider we cannot execute our LINQ Queries.
LINQ to Objects:
The LINQ to Objects provider allows us to query an in-memory object such as an array, collection, and generics types. It provides many built-in functions that we can use to perform many useful operations such as filtering, ordering, and grouping with minimum code.
LINQ to SQL (DLINQ):
The LINQ to SQL Provider is designed to work with only the SQL Server database. You can consider this as an object-relational mapping (ORM) framework which allows one to one mapping between the SQL Server database and related .NET Classes. These .NET classes are going to be created automatically by the wizard based on the database table.
LINQ to Datasets:
The LINQ to Datasets provider provides us the flexibility to query data cached in a Dataset in an easy and faster way. It also allows us to do further data manipulation operations such as searching, filtering, sorting, etc. on the Dataset using the LINQ Syntax.
LINQ to Entities:
The LINQ to Entities provider looks like LINQ to SQL. It means it is also an object-relational mapping (ORM) framework that allows one to one, one to many, and many to many mapping between the database tables and .NET Classes. The point that you need to remember is, it is used to query any database such as SQL Server, Oracle, MySQL, DB2, etc. Now, it is called ADO.NET Entity Framework.
LINQ to XML (XLINQ):
The LINQ to XML provider is basically designed to work with an XML document. So, it allows us to perform different operations on XML data sources such as querying or reading, manipulating, modifying, and saving the changes to XML documents. The System.Xml.Linq namespace contains the required classes for LINQ to XML.
Parallel LINQ (PLINQ):
The Parallel LINQ or PLINQ was introduced with .NET Framework 4.0. This provider provides the flexibility of parallel implementation of LINQ to Objects. The PLINQ was designed in such a way that it uses the power of parallel programming which targets the Task Parallel Library. So if you want to execute your LINQ query simultaneously or parallel on different processors then you need to write the query using PLINQ.
Advantages of using LINQ?
- We don’t need to learn new query language syntaxes for different data sources as it provides common query syntax to query different data sources.
- Less code as compared to the traditional approach. That means using LINQ we can minimize our code.
- It provides Compile time error checking as well as intelligence support in Visual Studio. This powerful feature helps us to avoid run-time errors.
- LINQ provides a lot of inbuilt methods that we can use to perform different operations such as filtering, ordering, grouping, etc. which makes our work easy.
- Its query can be reused.
Disadvantages of using LINQ?
The disadvantages of using LINQ are as follows:
- Using LINQ it’s very difficult to write complex queries like SQL.
- LINQ doesn’t take the full advantage of SQL features like cached execution plan for the stored procedure.
- We will get the worst performance if we don’t write the queries properly.
- If we do some changes to our queries, then we need to recompile the application and need to redeploy the dll to the server.
Conclusion
So far this article we discussed about LINQ Architecture, we discussed about linq architecture and all the linq properties.