ข้ามองค์ประกอบจากท้ายสุดและคืนค่าองค์ประกอบที่เหลือโดยใช้เมธอด SkipLast()
นี่คืออาร์เรย์:
int[] marks = { 45, 88, 50, 90, 95, 85 };
ตอนนี้ ให้เราข้าม 2 องค์ประกอบจากท้ายสุดโดยใช้ 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