C# Queryable Take() 方法


使用 Take() 方法從開頭獲取指定數量的元素。

以下是我們的陣列。

int[] marks = { 35, 72, 50, 90, 95, 85, 52, 67 };

現在,使用 OrderByDescending 按降序對元素進行排序。然後使用 Take() 方法獲取元素。

marks.AsQueryable().OrderByDescending(s => s).Take(5);

讓我們看一看完整的示例。

示例

 線上演示

using System;
using System.Linq;
using System.Collections.Generic;
public class Demo {
   public static void Main() {
      int[] marks = { 35, 72, 50, 90, 95, 85, 52, 67 };
      // top 5 student marks
      IEnumerable<int> selMarks = marks.AsQueryable().OrderByDescending(s => s).Take(5);
      foreach (int res in selMarks) {
         Console.WriteLine(res);
      }
   }
}

輸出

95
90
85
72
67

更新於: 23-6-2020

2K+ 瀏覽

開啟你的 職業生涯

完成課程獲取認證資格

開始行動
廣告
© . All rights reserved.