C# 可查询 TakeLast() 方法
使用该TakeLast()方法从末尾获取指定数量的元素。
以下是我们的数组。
int[] pages = { 492, 290, 129, 602, 152 };现在,使用OrderBy按升序对元素进行排序。然后使用该TakeLast()方法从末尾获取指定数量的元素。
marks.AsQueryable().OrderByDescending(s => s).Take(5);
让我们看看完整的例子。
示例
using System;
using System.Linq;
using System.Collections.Generic;
public class Demo {
public static void Main() {
//书页
int[] pages = { 492, 290, 129, 602, 152 };
//获取最后两本书的页面
IEnumerable last = pages.AsQueryable().OrderBy(s => s).TakeLast(2);
foreach (int res in last) {
Console.WriteLine(res);
}
}
} 输出结果492 602