LINQ C# LÀ GÌ

  -  

In this article

Language-Integrated Query (LINQ) is the name for a set of technologies based on the integration of query capabilities directly inlớn the C# language. Traditionally, queries against data are expressed as simple strings without type checking at compile time or IntelliSense support. Furthermore, you have lớn learn a different query language for each type of data source: Squốc lộ databases, XML documents, various Web services, và so on. With LINQ, a query is a first-class language construct, just lượt thích classes, methods, events. You write queries against strongly typed collections of objects by using language keywords & familiar operators. The LINQ family of technologies provides a consistent query experience for objects (LINQ to Objects), relational databases (LINQ to lớn SQL), & XML (LINQ to lớn XML).

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

For a developer who writes queries, the most visible "language-integrated" part of LINQ is the query expression. Query expressions are written in a declarative sầu query syntax. By using query syntax, you can perkhung filtering, ordering, & grouping operations on data sources with a minimum of code. You use the same basic query expression patterns to query & transsize data in SQL databases, ADO.NET Datasets, XML documents & streams, và .NET collections.

You can write LINQ queries in C# for SQL Server databases, XML documents, ADO.NET Datasets, và any collection of objects that supports IEnumerable or the generic IEnumerable interface. LINQ support is also provided by third parties for many Web services and other database implementations.

Xem thêm: Uat Testing Là Gì - Tìm Hiểu Ngay Nghĩa Và Những Thông Tin Về Uat

The following example shows the complete query operation. The complete operation includes creating a data source, defining the query expression, & executing the query in a foreach statement.

Xem thêm: Hệ Số Car Là Gì ? Nghĩa Của Từ Car Trong Tiếng Việt Car Nghĩa Là Gì Trong Tiếng Anh

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; // Exexinh tươi the query. foreach (int i in scoreQuery) Console.Write(i + " "); // Output: 97 92 81The following illustration from Visual Studio shows a partially-completed LINQ query against a Squốc lộ Server database in both C# và Visual Basic with full type checking & IntelliSense support:

*

Query expression overview

Query expressions can be used to query & to lớn transform data from any LINQ-enabled data source. For example, a single query can retrieve sầu data from a Squốc lộ database, và produce an XML stream as output.Query expressions are easy khổng lồ master because they use many familiar C# language constructs.As a rule when you write LINQ queries, we recommover that you use query syntax whenever possible and method syntax whenever necessary. There is no semantic or performance difference between the two different forms. Query expressions are often more readable than equivalent expressions written in method syntax.

Next steps

To learn more details about LINQ, start by becoming familiar with some basic concepts in Query expression basics, và then read the documentation for the LINQ giải pháp công nghệ in which you are interested: