TakeLast(n) (.NET 6+) trả về n phần tử cuối của chuỗi. Kết hợp OrderBy để lấy top lớn nhất.
Ví dụ minh họa
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 };
IEnumerable<int> units = prod.AsQueryable().TakeLast(3);
foreach (int res in units)
Console.WriteLine(res);
}
}
Kết quả đầu ra
698 765 789
Lưu ý: Ví dụ không sắp xếp, nên lấy 3 phần tử cuối theo thứ tự gốc. Muốn 3 giá trị lớn nhất: .OrderBy(x => x).TakeLast(3) hoặc .OrderByDescending(x => x).Take(3).