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

更新於:23-Jun-2020

2K+ 瀏覽量

開啟你的 職業生涯

完成課程後獲得認證

開始吧
廣告
© . All rights reserved.