Enumerable.Take 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
重载
Take<TSource>(IEnumerable<TSource>, Int32) |
从序列的开头返回指定数量的相邻元素。 |
Take<TSource>(IEnumerable<TSource>, Range) |
返回序列中连续元素的指定范围。 |
Take<TSource>(IEnumerable<TSource>, Int32)
从序列的开头返回指定数量的相邻元素。
public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
static System::Collections::Generic::IEnumerable<TSource> ^ Take(System::Collections::Generic::IEnumerable<TSource> ^ source, int count);
public static System.Collections.Generic.IEnumerable<TSource> Take<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, int count);
static member Take : seq<'Source> * int -> seq<'Source>
<Extension()>
Public Function Take(Of TSource) (source As IEnumerable(Of TSource), count As Integer) As IEnumerable(Of TSource)
类型参数
- TSource
source
的元素类型。
参数
- source
- IEnumerable<TSource>
要从其返回元素的序列。
- count
- Int32
要返回的元素数量。
返回
- IEnumerable<TSource>
一个 IEnumerable<T>,包含输入序列开头的指定数量的元素。
例外
source
为 null
。
示例
下面的代码示例演示如何使用 Take 它从序列的开头返回元素。
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
*/
' Create an array of Integer values that represent grades.
Dim grades() As Integer = {59, 82, 70, 56, 92, 98, 85}
' Get the highest three grades by first sorting
' them in descending order and then taking the
' first three values.
Dim topThreeGrades As IEnumerable(Of Integer) =
grades _
.OrderByDescending(Function(grade) grade) _
.Take(3)
' Display the results.
Dim output As New System.Text.StringBuilder("The top three grades are:" & vbCrLf)
For Each grade As Integer In topThreeGrades
output.AppendLine(grade)
Next
Console.WriteLine(output.ToString())
' This code produces the following output:
'
' The top three grades are:
' 98
' 92
' 85
注解
此方法通过使用延迟执行来实现。 即时返回值是一个对象,用于存储执行操作所需的所有信息。 只有在通过直接调用其方法或在 Visual C# 中For Each
或Visual Basic中foreach
调用该GetEnumerator
对象,否则不会执行此方法表示的查询。
Takesource
枚举和生成元素,直到count
生成元素或source
不包含更多元素。 如果 count
超出元素的数量 source
,则返回所有元素 source
。
如果 count
小于或等于零, source
则不枚举并返回空 IEnumerable<T> 。
和TakeSkip方法是功能补充。 给定集合序列coll
和整数n
,连接并coll.Skip(n)
生成与同一序列coll
的结果coll.Take(n)
。
在Visual Basic查询表达式语法中,子Take
句转换为调用 Take。
另请参阅
适用于
Take<TSource>(IEnumerable<TSource>, Range)
返回序列中连续元素的指定范围。
public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
static System::Collections::Generic::IEnumerable<TSource> ^ Take(System::Collections::Generic::IEnumerable<TSource> ^ source, Range range);
public static System.Collections.Generic.IEnumerable<TSource> Take<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Range range);
static member Take : seq<'Source> * Range -> seq<'Source>
<Extension()>
Public Function Take(Of TSource) (source As IEnumerable(Of TSource), range As Range) As IEnumerable(Of TSource)
类型参数
- TSource
source
的元素类型。
参数
- source
- IEnumerable<TSource>
要从其返回元素的序列。
- range
- Range
要返回的元素范围,该元素的起始索引和结束索引从序列的开头或末尾开始。
返回
- IEnumerable<TSource>
一个包含序列中source
指定元素范围的元素IEnumerable<T>。
例外
source
上声明的默认值为 null
。
注解
此方法通过使用延迟执行来实现。 即时返回值是一个对象,用于存储执行操作所需的所有信息。 只有在通过直接调用其方法或在 Visual C# 中For Each
或Visual Basic中foreach
调用该GetEnumerator
对象,否则不会执行此方法表示的查询。
Take 枚举 source
并生成其索引属于指定 range
元素的元素。