Developers across the world have always encountered problems in querying data because of the lack of a defined path & need lớn master a multiple of technologies like SQL, website Services, XQuery, etc.

Bạn đang xem: Linq c# là gì

Introduced in Visual Studio 2008 & designed by Anders Hejlsberg, LINQ (Language Integrated Query) allows writing queries even without the knowledge of query languages lượt thích SQL, XML etc. LINQ queries can be written for diverse data types.

Example of a LINQ query


using System;using System.Linq;class Program { static void Main() { string<> words = "hello", "wonderful", "LINQ", "beautiful", "world"; //Get only short words var shortWords = from word in words where word.Length


Module Module1 Sub Main() Dim words As String() = "hello", "wonderful", "LINQ", "beautiful", "world" " Get only short words Dim shortWords = From word In words _ Where word.Length When the above code of C# or VB is compiled & executed, it produces the following result −

hello LINQ world

Syntax of LINQ

There are two syntaxes of LINQ. These are the following ones.

Lamda (Method) Syntax

var longWords = words.Where( w ⇒ w.length > 10);Dim longWords = words.Where(Function(w) w.length > 10)

Query (Comprehension) Syntax

var longwords = from w in words where w.length > 10;Dim longwords = from w in words where w.length > 10

Types of LINQ

The types of LINQ are mentioned below in brief.

LINQ to lớn ObjectsLINQ khổng lồ XML(XLINQ)LINQ to DataSetLINQ lớn SQL (DLINQ)LINQ khổng lồ Entities

Apart from the above, there is also a LINQ type named PLINQ which is Microsoft’s parallel LINQ.

LINQ Architecture in .NET

LINQ has a 3-layered architecture in which the uppermost layer consists of the language extensions và the bottom layer consists of data sources that are typically objects implementing IEnumerable or IQueryable generic interfaces. The architecture is shown below.


Query Expressions

Query expression is nothing but a LINQ query, expressed in a form similar to lớn that of SQL with query operators like Select, Where và OrderBy. Query expressions usually start with the keyword "From".

To access standard LINQ query operators, the namespace System.Query should be imported by default. These expressions are written within a declarative query syntax which was C# 3.0.

Below is an example to show a complete query operation which consists of data source creation, query expression definition and query execution.


using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Operators class LINQQueryExpressions static void Main() // Specify the data source. Int<> scores = new int<> 97, 92, 81, 60 ; // Define the query expression. IEnumerable scoreQuery = from score in scores where score > 80 select score; // Execute the query. Foreach (int i in scoreQuery) Console.Write(i + " "); Console.ReadLine(); When the above code is compiled và executed, it produces the following result −

97 92 81

Extension Methods

Introduced with .NET 3.5, Extension methods are declared in static classes only & allow inclusion of custom methods khổng lồ objects to lớn perform some precise query operations lớn extend a class without being an actual thành viên of that class. These can be overloaded also.

Xem thêm: Quy Trình Công Nghệ Sản Xuất Gạch Ceramic Của Viglacera Hàng Đầu Thế Giới

In a nutshell, extension methods are used to translate query expressions into traditional method calls (object-oriented).

Difference between LINQ & Stored Procedure

There is an array of differences existing between LINQ and Stored procedures. These differences are mentioned below.

Stored procedures are much faster than a LINQ query as they follow an expected execution plan.

It is easy lớn avoid run-time errors while executing a LINQ query than in comparison to lớn a stored procedure as the former has Visual Studio’s Intellisense support as well as full-type checking during compile-time.

LINQ allows debugging by making use of .NET debugger which is not in case of stored procedures.

LINQ offers tư vấn for multiple databases in contrast to stored procedures, where it is essential lớn re-write the code for diverse types of databases.

Deployment of LINQ based solution is easy & simple in comparison lớn deployment of a mix of stored procedures.

Need For LINQ

Prior khổng lồ LINQ, it was essential to lớn learn C#, SQL, and various APIs that bind together the both to form a complete application. Since, these data sources and programming languages face an impedance mismatch; a need of short coding is felt.

Below is an example of how many diverse techniques were used by the developers while querying a data before the advent of LINQ.

SqlConnection sqlConnection = new SqlConnection(connectString);SqlConnection.Open();System.Data.SqlClient.SqlCommand sqlCommand = new SqlCommand();sqlCommand.Connection = sqlConnection;sqlCommand.CommandText = "Select * from Customer";return sqlCommand.ExecuteReader (CommandBehavior.CloseConnection) Interestingly, out of the featured code lines, query gets defined only by the last two. Using LINQ, the same data query can be written in a readable color-coded form like the following one mentioned below that too in a very less time.