LINQ Introduction - What is LINQ & Why we use LINQ

Here we will learn the introduction to LINQ, what is linq in c# / asp.net, why we use LINQ in c# / asp.net, LINQ architecture with diagram, advantages of LINQ, disadvantages of LINQ, etc. in detail with examples in c# and vb.net.

What is LINQ?

The complete form of LINQ is Language Integrated Query and it was introduced in .NET Framework 3.5 to query the data from different data sources such as collectionsgenerics, XML Documents, ADO.NET Datasets, SQL, Web Service, etc., in C# and VB.NET. The LINQ will provide a rich, standardized query syntax in .NET programming languages such as C# and VB.NET to allow the developers to interact with any data.

 

In c# or vb.net, the LINQ functionality can be achieved by importing System.Linq namespace in our application. Generally, the LINQ will contain a set of extension methods that allow you to query the data source objects directly in the code based on our requirements.

LINQ Architecture

The following diagram will represent the complete architectural view of LINQ.

 

LINQ Architectural View Diagram

As mentioned in the above diagram, the various LINQ objects are available in c# and vb.net.

 

  • LINQ to Objects
  • LINQ to Datasets
  • LINQ to SQL
  • LINQ to Entities
  • LINQ to XML

We will be covering these topics in detail in the later part of the series.

Why do we need to use LINQ?

Now the question arises why we should use LINQ? What’s so different in LINQ that everyone would want to use it in their application and not the SQL? Well, the answer is quite simple as LINQ is simpler, tidier, and higher-level than SQL. When it comes to querying databases, In most cases, LINQ is a more productive querying language than SQL.

 

Also, we have the benefit of IntelliSense as the LINQ query is written in code behind, and it has full type checking at compile time so we can catch any error in compile time itself. In c# or vb.net, writing queries is more fun in LINQ, but it has its advantages as well as disadvantages which are as follows:

Advantages of LINQ

Following are the advantages of using LINQ in our applications.

 

  1. It has full type checking at compile time.
  2. It has IntelliSense so that we can avoid silly errors.
  3. Its query can be reused.
  4. We can debug it using a .NET debugger.
  5. It supports filtering, sorting, ordering, grouping with much less effort. 

Disadvantages of LINQ

Following are the drawbacks of using LINQ in our applications.

 

  1. Since it is written in the code, we cannot use the Cache execution plan, which is a SQL feature as we do in the stored procedure.
  2. Writing complex queries in LINQ is a bit tedious as compared to SQL.
  3. If the query is not write correctly, then performance is degraded.
  4. We have to build the project and deploy the DLL whenever we make changes in the query.

LINQ has its advantages and disadvantages, so based on requirements, we can use LINQ in our applications.