รับจำนวนองค์ประกอบที่ระบุจากจุดสิ้นสุดโดยใช้เมธอด TakeLast()
ต่อไปนี้เป็นอาร์เรย์ของเรา
int[] pages = { 492, 290, 129, 602, 152 }; ตอนนี้ ใช้ OrderBy เพื่อเรียงลำดับองค์ประกอบจากน้อยไปมาก จากนั้นใช้เมธอด TakeLast() เพื่อรับองค์ประกอบตามจำนวนที่ระบุจากจุดสิ้นสุด
marks.AsQueryable().OrderByDescending(s => s).Take(5);
เรามาดูตัวอย่างฉบับสมบูรณ์กัน
ตัวอย่าง
using System;
using System.Linq;
using System.Collections.Generic;
public class Demo {
public static void Main() {
// pages of books
int[] pages = { 492, 290, 129, 602, 152 };
// get pages of last two books
IEnumerable<int> last = pages.AsQueryable().OrderBy(s => s).TakeLast(2);
foreach (int res in last) {
Console.WriteLine(res);
}
}
} ผลลัพธ์
492 602