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
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
安卓
Python
C 语言编程
C++
C#
MongoDB
MySQL
JavaScript
PHP