Take
Take操作符用于从输入序列中返回指定数量的元素。用法如下代码:
static void Main()
{
int[] grades = { 59, 82, 70, 56, 92, 98, 85 };
//对数组降序排序并取出前三个记录
IEnumerable<int> topThreeGrades =
grades.OrderByDescending(grade => grade).Take(3);
Console.WriteLine("The top three grades are:");
foreach (int grade in topThreeGrades)
{
Console.WriteLine(grade);
}
}
运行结果为:
The top three grades are:
98
92
85