FIRSTORDEFAULT LÀ GÌ

  -  

Nếu bạn đã có lần sử dụng C# Linq để viết truy vấn vấn dữ liệu trong IEnumrable thì chắc hẳn các bạn sẽ rất quen ở trong với bốn phương thức là: Single, SingleOrDefault, First, FirstOrDefault đúng không nào :). Mặc dù nhiên, ví như không hiểu rõ về chúng, bạn sẽ tương đối dễ sử dụng sai lắm đấy nhé.

Bạn đang xem: Firstordefault là gì

Chuẩn bị dữ liệu

Để đơn giản dễ dàng và dễ dàng hình dung, mình sẽ tạo nên một Dummy Data nho bé dại cho kiểm tra như sau:

Nội dung bỏ ra tiết

Single

Giải thích: tra cứu trong danh sách L một trong những phần tử đối chọi duy độc nhất vô nhị thỏa điều kiện C:

Nếu không kiếm thấy: (1)Throw exception –> Sequence contains no matching elementDone.Nếu tra cứu thấy:Tìm thấy duy nhất một trong những phần tử: (2)Trả về bộ phận đóDoneTìm thấy nhiều hơn một phần tử: (3)Throw exception –> Sequence contains more than one matching elementDone

Demo:

var products = DummyData.GetProducts();// Case (1)var productCase1 = products.Single(p => p.Id == 2000);// Case (2)var productCase2 = products.Single(p => p.Id == 1000);// Case (3)var productCase3 = products.Single(p => p.Name.Contains("iPhone 6"));

SingleOrDefault

Giải thích: Tìm trong danh sách L một phần tử đối kháng duy nhất thỏa đk C:

Nếu không kiếm thấy: (4)Trả về giá trị mặc định của vẻ bên ngoài dữ liệu phần tử cần tìm kiếm (xem phụ lục sinh hoạt cuối bài)Done.Nếu search thấy:Tìm thấy duy nhất 1 phần tử: (5)Trả về thành phần đóDoneTìm thấy nhiều hơn một phần tử: (6)Throw exception –> Sequence contains more than one matching elementDone

Note: khi sử dụng SingleOrDefault đề xuất viết sinh sống dạng cú pháp Null Coalesce vẫn gọn hơn.

Demo: (Case 4)

// Get datavar products = DummyData.GetProducts();// Get hàng hóa name using syntax Null Coalesce// If value != null return value.Name Else return "Not Found"var productName = products.SingleOrDefault(x => x.Id == 2000)?.Name ?? "Not Found!";Như trong lấy ví dụ như trên, kết quả của productName vẫn là Not Found vì không tồn tại product nào tất cả Id bởi 2000. Lúc ấy nó đang trả về quý hiếm mặc định của object sản phẩm là null. Mà lại như quy cầu trong code thì chạm mặt null sẽ trả về Not Found.

First

Giải thích: Trong danh sách L, trả về phần tử đầu tiên thỏa điều kiện C (có thể bao gồm nhiều bộ phận cùng thỏa điều kiện C):

Nếu không tìm kiếm thấy: (7)Throw exception –> Sequence contains no matching elementDone.Nếu tìm kiếm thấy: (8)Trả về thành phần đầu tiên trong công dụng tìm được.Done.

Xem thêm: Check Valve Là Gì ? Van 1 Chiều Là Gì? Tìm Hiểu Một Số Loại Van Một Chiều Cơ Bản

Demo:

var products = DummyData.GetProducts();// Case 7var p1 = products.First(x => x.Name.Contains("iPhone X"));// Case 8var p2 = products.First(x => x.Name.Contains("iPhone 6"));

FirstOrDefault

Giải thích: Trong danh sách L, trả về phần tử đầu tiên thỏa đk C (có thể gồm nhiều bộ phận cùng thỏa đk C):

Nếu không kiếm thấy: (9)Trả về giá trị mặc định của dạng hình dữ liệu phần tử cần tìm kiếm (xem phụ lục làm việc cuối bài)Done.Nếu tìm thấy: (10)Trả về thành phần đầu tiên trong kết quả tìm được.Done.

Demo:

var products = DummyData.GetProducts();// Case 9 --> return null;var p1 = products.FirstOrDefault(x => x.Name.Contains("iPhone X"))// Case 10var p2 = products.FirstOrDefault(x => x.Name.Contains("iPhone 6"))Trong lấy ví dụ như trên, tác dụng của p1 sẽ là giá trị mặc định của object Product. Vì chưng đó, p1 sẽ bởi null.

Phục lục (C# Language Specification 5.0)

The default value of a variable depends on the type of the variable & is determined as follows:

For a variable of a value-type, the default value is the same as the value computed by the value-type’s default constructor.For a variable of a reference-type, the default value is null.

Xem thêm: Công Thức Tính Hiệu Suất Là Gì ? Công Thức Tính Hiệu Suất Phương Pháp Giúp Đo Lường Hiệu Suất Công Việc

Default constructors

All value types implicitly declare a public parameterless instance constructor called the default constructor. The mặc định constructor returns a zero-initialized instance known as the default value for the value type:

For all simple-types, the default value is the value produced by a bit pattern of all zeros:For sbyte, byte, short, ushort, int, uint, long, và ulong, the default value is 0.For char, the mặc định value is ‘x0000’.For float, the default value is 0.0f.For double, the mặc định value is 0.0d.For decimal, the mặc định value is 0.0m.For bool, the default value is false.For an enum-type E, the mặc định value is 0, converted to the type E.For a struct-type, the mặc định value is the value produced by setting all value type fields to lớn their default value and all reference type fields to null.For a nullable-type the mặc định value is an instance for which the HasValue property is false và the Value property is undefined. The default value is also known as the null value of the nullable type.

Tài liệu tham khảo