LINQ – Language Integrated Query

1 min read

Source: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/ 

Language-Integrated Query (LINQ) là tên của một tập hợp các công nghệ dựa trên việc tích hợp các khả năng truy vấn trực tiếp vào ngôn ngữ lập trình.

Có thể được sử dụng để trích xuất và xử lý dữ liệu từ bất kỳ đối tượng nào triển khai IEnumerable<T> interface.

LINQ cung cấp trải nghiệm truy vấn nhất quán cho các đối tượng (LINQ to Object), cơ sở dữ liệu quan hệ (LINQ to SQL) và XML (LINQ to XML).

Những khái niệm bạn nên biết trước khi đến với LINQ:

Generic types and IEnumerable<T> interface

Source: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/generics/ 

  • Generics cho phép bạn viết mã có mục đích chung là loại an toàn tại thời điểm biên dịch bằng cách sử dụng cùng một loại ở nhiều nơi mà không cần biết trước loại đó là gì..
linq
  • IEnumerable<T> is the interface that enables generic collection classes to be enumerated by using the foreach statement.

Delegates

Source: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/delegates/ 

  • Một kiểu đại diện cho các tham chiếu đến các phương thức có danh sách tham số và kiểu trả về cụ thể.
  • Được sử dụng để truyền các phương thức làm đối số cho các phương thức khác..
  • Có thể gọi phương thức bằng cách sử dụng delegates.

Lambda Expressions

Source: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/lambda-expressions 

  • Cách ngắn hơn để viết các anonymous methods.
  • Basic syntax: (input-parameters) => expression

Extension methods

Source: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/extension-methods 

  • Cho phép thêm phương thức vào các loại được xác định mà không cần tạo loại dẫn xuất mới hoặc sửa đổi loại ban đầu.

Syntax

  • Query syntax:
  • Method syntax:

Standard operations:

Bạn có thể tham khảo dưới bảng sau và tìm hiểu thêm về nó:

RestrictionWhere
ProjectionSelect, SelectMany
PartitioningTake, Skip, TakeWhile, SkipWhile
GroupingOrderBy, OrderByDescending, ThenBy, ThenByDescending, Reverse
OrderingGroupBy
SetDistinct, Union, Intersect, Except
ConversionToArray, ToList, ToDictionary, OfType
ElementFirst, FirstOrDefault, ElementAt, Last, LastOrDefault
GenerationRange, Repeat
QuantifierAny, All
AggregateCount, Sum, Min, Max, Average, Aggregate
JoinJoin, GroupJoin
Avatar photo

Leave a Reply

Your email address will not be published. Required fields are marked *