Sử dụng phương thức TakeLast () để trả về các phần tử từ cuối mảng.
Đầu tiên chúng ta hãy khai báo và khởi tạo một mảng.
int[] prod = { 110, 290, 340, 540, 456, 698, 765, 789}; Bây giờ, hãy lấy ba yếu tố cuối cùng.
IEnumerable<int> units = prod.AsQueryable().TakeLast(3);
Hãy cho chúng tôi xem mã hoàn chỉnh.
Ví dụ
using System;
using System.Linq;
using System.Collections.Generic;
public class Demo {
public static void Main() {
int[] prod = { 110, 290, 340, 540, 456, 698, 765, 789};
// value of last three products
IEnumerable<int> units = prod.AsQueryable().TakeLast(3);
foreach (int res in units) {
Console.WriteLine(res);
}
}
} Đầu ra
698 765 789