C# Queryable SkipWhile() 方法


使用 SkipWhile() 方法,繞過陣列中的某些元素並返回剩餘元素。

下面是我們的陣列 −

int[] marks = { 45, 88, 55, 90, 95, 85 };

現在,我們跳過大於或等於 60 的元素。我們使用 Lambda 表示式設定了條件。

IEnumerable<int> selMarks = marks.AsQueryable().OrderByDescending(s => s).SkipWhile(s => s >= 60);

示例

 即時演示

using System;
using System.Linq;
using System.Collections.Generic;
public class Demo {
   public static void Main() {
      int[] marks = { 45, 88, 55, 90, 95, 85 };
      // skips elements above 60
      IEnumerable<int> selMarks = marks.AsQueryable().OrderByDescending(s => s).SkipWhile(s => s >= 60);
      // displays rest of the elements
      Console.WriteLine("Skipped marks > 60...");
      foreach (int res in selMarks) {
         Console.WriteLine(res);
      }
   }
}

輸出

Skipped marks > 60...
55
45

更新於: 23-6-2020

116 次瀏覽

開啟您的 職業生涯

完成課程以獲得認證

開始
廣告
© . All rights reserved.