Enumerable.Take<TSource> 方法
从序列的开头返回指定数量的连续元素。
程序集: System.Core(在 System.Core.dll 中)
下面的代码示例演示如何使用 Take<TSource> 从序列的开头返回元素。
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);
}
/*
结果
This code produces the following output:
The top three grades are:
98
92
85