C#程序从序列开头返回指定数量的元素
设置一个数组,并使用OrderByDescending按降序排列。
int[] prod = { 290, 340, 129, 540, 456, 898, 765, 789, 345};现在,使用Take()方法从开头返回指定数量的元素。
Enumerable<int> units = prod.AsQueryable().OrderByDescending(s => s).Take(2);
让我们看看完成的代码。
示例
using System;
using System.Linq;
using System.Collections.Generic;
public class Demo {
public static void Main() {
int[] prod = { 290, 340, 129, 540, 456, 898, 765, 789, 345};
// Volume of top two products
IEnumerable<int> units = prod.AsQueryable().OrderByDescending(s => s).Take(2);
foreach (int res in units) {
Console.WriteLine(res);
}
}
}输出
898 789
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C编程
C++
C#
MongoDB
MySQL
Javascript
PHP