Enumerable.Take 메서드

정의

오버로드

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

설명

이 메서드는 지연 된 실행을 사용 하 여 구현 됩니다. 즉시 반환 값은 작업을 수행 하는 데 필요한 모든 정보를 저장 하는 개체입니다. 이 메서드가 나타내는 쿼리는 개체를 직접 호출 GetEnumerator 하거나 C# 또는 For Each Visual Basic에서 를 사용하여 foreach 개체를 열거할 때까지 실행되지 않습니다.

Take요소가 생성되거나 source 더 이상 요소가 포함되지 않을 때까지 count 요소를 열거 source 하고 생성합니다. 의 요소 source수를 초과하면 countsource 모든 요소가 반환됩니다.

가 0 source 보다 작거나 같으면 count 가 열거되지 않고 빈 IEnumerable<T> 가 반환됩니다.

Skip 메서드는 Take 기능 보완입니다. 컬렉션 시퀀스와 colln수 가 지정되면 의 결과를 coll.Take(n) 연결하고 와 coll.Skip(n) 동일한 시퀀스를 coll생성합니다.

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>

IEnumerable<T> 시퀀스에서 지정된 요소 범위를 포함하는 입니다source.

예외

sourcenull입니다.

설명

이 메서드는 지연 된 실행을 사용 하 여 구현 됩니다. 즉시 반환 값은 작업을 수행 하는 데 필요한 모든 정보를 저장 하는 개체입니다. 이 메서드가 나타내는 쿼리는 개체를 직접 호출 GetEnumerator 하거나 C# 또는 For Each Visual Basic에서 를 사용하여 foreach 개체를 열거할 때까지 실행되지 않습니다.

Take인덱스가 지정된 rangesource 속하는 요소를 열거하고 생성합니다.

적용 대상