ใช้วิธี TakeLast() เพื่อส่งคืนองค์ประกอบจากจุดสิ้นสุดของอาร์เรย์
ให้เราประกาศและเริ่มต้นอาร์เรย์ก่อน
int[] prod = { 110, 290, 340, 540, 456, 698, 765, 789}; มาดูองค์ประกอบสามข้อสุดท้ายกัน
IEnumerable<int> units = prod.AsQueryable().TakeLast(3);
ให้เราดูรหัสที่สมบูรณ์
ตัวอย่าง
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);
}
}
} ผลลัพธ์
698 765 789