C# Linq SkipLast 方法
從末尾跳過元素,然後使用 SkipLast() 方法返回其餘元素。
下面是一個數組。
int[] marks = { 45, 88, 50, 90, 95, 85 };現在,讓我們使用 SkipLast() 和 Lambda 表示式跳過末尾的兩個元素,但在對元素進行降序排列之後執行該操作。
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
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP