Message bên trên là thông báo lổi khi bạn cố gắng tìm 1 item được chỉ định trong 1 sequence sử dụng Linq, các phương thức thường cause lổi đó bao gồm các extension method sau: First, Last, hoặc Single, nào chúng ta cùng rảo qua thí dụ sau nhen:
Tạo 1 ứng dụng console C# và VB.NET:
Cũng là 1 class City simple:
C#:
public class City
{
public string Name { get; set; }
public string Country { get; set; }
}
VB.NET:
Class City
Public Property Name As String
Public Property Country As String
End Class
Sau đó khởi tạo tập hợp các City trong hàm Main
C#:
var cities = new List<City>
{
new City {Country = "Vietnam", Name = "Ha Noi"},
new City {Country = "USA", Name = "New York"},
new City {Country = "USA", Name = "Sri lanka"},
new City {Country = "Japan", Name = "Tokyo"},
new City {Country = "Spain", Name = "Milan"},
new City {Country = "Spain", Name = "Real Madrid"},
new City {Country = "Spain", Name = "Barcelona"},
new City {Country = "Netherland", Name = "Amsterdam"},
new City {Country = "Vietnam", Name = "Ho Chi Minh"},
};
VB.NET:
Dim cities As New List(Of City)() From
{
New City With {.Country = "Vietnam", .Name = "Ha Noi"},
New City With {.Country = "USA", .Name = "New York"},
New City With {.Country = "Japan", .Name = "Tokyo"},
New City With {.Country = "Spain", .Name = "Milan"},
New City With {.Country = "Spain", .Name = "Real Madrid"},
New City With {.Country = "Spain", .Name = "Barcelona"},
New City With {.Country = "Netherland", .Name = "Amsterdam"},
New City With {.Country = "Vietnam", .Name = "Ho Chi Minh"}
}
Cuối cùng là mình sẽ tìm 1 specific element trong sequence cities như sau:
C#:
var singleOrDefaultOperator = cities.Where(c => c.Country == "China").SingleOrDefault();
var fistOrDefaultOperator = cities.Where(c => c.Country == "Vietnam").FirstOrDefault();
var lastOrDefaultOperator = cities.Where(c => c.Country == "Vietnam").LastOrDefault();
var fistOperator = cities.Where(c => c.Country == "Vietnam").First();
var lastOperator = cities.Where(c => c.Country == "Vietnam").Last();
var fistOperator1 = cities.Where(c => c.Country == "China").First();
var lastOperator1 = cities.Where(c => c.Country == "China").Last();
var singleOperator = cities.Where(c => c.Country == "China").Single();
VB.NET:
Dim singleOrDefaultOperator = cities.Where(Function(c) c.Country = "China").SingleOrDefault()
Dim firstOrDefaultOperator = cities.Where(Function(c) c.Country = "Vietnam").FirstOrDefault()
Dim lastOrDefaultOperator = cities.Where(Function(c) c.Country = "Vietnam").LastOrDefault()
Dim fistOperator = cities.Where(Function(c) c.Country = "Vietnam").First()
Dim lastOperator = cities.Where(Function(c) c.Country = "Vietnam").Last()
Dim fistOperator1 = cities.Where(Function(c) c.Country = "China").First()
Dim lastOperator1 = cities.Where(Function(c) c.Country = "China").Last()
Dim singleOperator = cities.Where(Function(c) c.Country = "China").Single()
Như chúng ta đã thấy câu biến singleOrDefaultOperator sẽ trả về null, vì chúng ta dùng SingleOrDefault extension method, nếu element đó có tồn tại trong sequence thì trả về element đó, ngược lại trả về null, điều này có ích khi trong code của chúng ta muốn có giá trị mặc định cho 1 variable nào đó. Tương tự cho FirstOrDefault và LastOrDefault
Lúc này giá trị của biến firstOrDefaultOperator sẽ là: Country: Vietnam, và Name: Ha Noi (item đầu tiên)
Lúc này giá trị của biến lastOrDefaultOperator sẽ là: Country: Vietnam, và Name: Ho Chi Minh (item cuối cùng)
Bởi vì trong sequence cities của chúng ta có tồn tại Country Vietnam nên các biến firstOperator và lastOperator sẽ có giá trị giống như firstOrDefaultOperator và lastOrDefaultOperator.
3 câu lệnh cuối sẽ quăng exception vì chúng không tồn tại trong sequence. (Sequence contains no elements trong linq)
Hope this help