ข้ามองค์ประกอบจากจุดสิ้นสุดและส่งคืนองค์ประกอบที่เหลือโดยใช้วิธี SkipLast()
ต่อไปนี้เป็นอาร์เรย์
int[] marks = { 45, 88, 50, 90, 95, 85 }; ตอนนี้ ให้เราข้ามสององค์ประกอบจากจุดสิ้นสุดโดยใช้ SkipLast() และ Lambda Expressions แต่สิ่งนี้จะทำหลังจากจัดเรียงองค์ประกอบในลำดับจากมากไปน้อย
IEnumerable<int> selMarks = marks.AsQueryable().OrderByDescending(s => s).SkipLast(2);
ตัวอย่าง
using System;
using System.Linq;
using System.Collections.Generic;
public class Demo {
public static void Main() {
int[] marks = { 45, 88, 50, 90, 95, 85 };
IEnumerable<int> selMarks = marks.AsQueryable().OrderByDescending(s => s).SkipLast(2);
Console.WriteLine("Skipped the marks of last two students...");
foreach (int res in selMarks)
Console.WriteLine(res);
}
} ผลลัพธ์
Skipped the marks of last two students... 95 90 88 85