Open In App

LINQ (Language Integrated Query)

Last Updated : 21 May, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

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.

Similar Reads

LINQ | Query Syntax
LINQ query syntax is consist of a set of query keywords defined into the .NET Framework version 3.5 or Higher. This allows the programmer or developers to write the commands similar to SQL style in the code(C# or VB.NET) without using quotes. It is also known as the Query Expression Syntax. In LINQ, you can write the query to IEnumerable collection
2 min read
What is Query in LINQ?
A query is an expression which is used to recover data from the data source. Generally, queries are expressed in some specialized language. Different types of languages are developed to access the different type of data sources like SQL for a relational database, XQuery for XML, etc. So, every time the developer needs to learn different type of lan
3 min read
C# Program to Generate Numbers that are Multiples of 5 Using the LINQ Parallel Query
LINQ is known as Language Integrated Query and it is introduced in .NET 3.5. It gives the ability to .NET languages to generate queries to retrieve data from the data source. It removes the mismatch between programming languages and databases and the syntax used to create a query is the same no matter which type of data source is used. In this arti
2 min read
C# Program to Generate Random Even Numbers Using LINQ Parallel Query
LINQ is known as Language Integrated Query and was introduced in .NET 3.5. It gives power to .NET languages to generate or create queries to retrieve data from the data source. In this article, we will generate random even numbers in parallel using LINQ. So, we will use ParallelQuery{TSource} to generate a sequence of integral numbers in parallel w
2 min read
C# Program to Join Employee and Department Class using LINQ Join Query
Given two classes named as Employee and Department, now we join Employee and Department class with the help of LINQ join Query. So to this task, we use the Join clause. This clause is used to join two data sources into one source which has some common attributes. It always takes two data sources and the result of the join depends upon which type o
3 min read
How to Convert SQL to LINQ Query?
A group of technologies known as Language-Integrated Query (LINQ) is built on the direct integration of query functionality into the C# language. The query expression is the component of LINQ that stands out the most to developers who write LINQ queries. The declarative query syntax is used to write query expressions. Filtering, ordering, and group
2 min read
CIL or MSIL | Microsoft Intermediate Language or Common Intermediate Language
The Microsoft Intermediate Language (MSIL), also known as the Common Intermediate Language (CIL) is a set of instructions that are platform independent and are generated by the language-specific compiler from the source code. The MSIL is platform independent and consequently, it can be executed on any of the Common Language Infrastructure supported
3 min read
C# Program to Demonstrate the Example of LINQ Union() Method with StringComparer
LINQ is known as Language Integrated Query and it is introduced in .NET 3.5. It gives a feature to .NET languages to create queries to retrieve data from the data source. Here in this article, we will demonstrate the example of the LINQ Union() method with the StringComparer. 1. Union() Method: This method is used to get the unique elements from th
2 min read
LINQ | Filtering Operator | OfType
Filtering operators are those operators which are used to filter the data according to the user requirement from the given data source or from the given sequence. For example, in an employee record, we want to get the data of the employees whose age in 21. So, we filter the record, according to their age. In LINQ, you can filter using the following
3 min read
LINQ | Method Syntax
In LINQ, Method Syntax is used to call the extension methods of the Enumerable or Queryable static classes. It is also known as Method Extension Syntax or Fluent. However, the compiler always converts the query syntax in method syntax at compile time. It can invoke the standard query operator like Select, Where, GroupBy, Join, Max, etc. You are all
2 min read
Article Tags :