Queryable.Take<TSource>(IQueryable<TSource>, Int32) 메서드

정의

시퀀스 시작 위치에서 지정된 수의 연속 요소를 반환합니다.Returns a specified number of contiguous elements from the start of a sequence.

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static System::Linq::IQueryable<TSource> ^ Take(System::Linq::IQueryable<TSource> ^ source, int count);
public static System.Linq.IQueryable<TSource> Take<TSource> (this System.Linq.IQueryable<TSource> source, int count);
static member Take : System.Linq.IQueryable<'Source> * int -> System.Linq.IQueryable<'Source>
<Extension()>
Public Function Take(Of TSource) (source As IQueryable(Of TSource), count As Integer) As IQueryable(Of TSource)

형식 매개 변수

TSource

source 요소의 형식입니다.The type of the elements of source.

매개 변수

source
IQueryable<TSource>

요소가 반환되는 시퀀스입니다.The sequence to return elements from.

count
Int32

반환할 요소 수입니다.The number of elements to return.

반환

IQueryable<TSource>

source 시작 위치에서 지정된 수의 요소가 들어 있는 IQueryable<T>입니다.An IQueryable<T> that contains the specified number of elements from the start of source.

예외

source이(가) null인 경우source is null.

예제

다음 코드 예제에서는를 사용 하 여 Take<TSource>(IQueryable<TSource>, Int32) 시퀀스의 시작 부분에서 요소를 반환 하는 방법을 보여 줍니다.The following code example demonstrates how to use Take<TSource>(IQueryable<TSource>, Int32) to return elements from the start of a sequence.

int[] grades = { 59, 82, 70, 56, 92, 98, 85 };

// Sort the grades in descending order and take the first three.
IEnumerable<int> topThreeGrades =
    grades.AsQueryable().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
*/

Dim grades() As Integer = {59, 82, 70, 56, 92, 98, 85}

' Sort the grades in descending order and take the first three.
Dim topThreeGrades = _
    grades.AsQueryable().OrderByDescending(Function(grade) grade).Take(3)

Dim output As New System.Text.StringBuilder
output.AppendLine("The top three grades are:")
For Each grade As Integer In topThreeGrades
    output.AppendLine(grade)
Next

' Display the output.
MsgBox(output.ToString())

' This code produces the following output:

' The top three grades are:
' 98
' 92
' 85

설명

Take<TSource>(IQueryable<TSource>, Int32)메서드는 자신을 MethodCallExpression 생성 된 제네릭 메서드로 호출 하는을 생성 합니다 Take<TSource>(IQueryable<TSource>, Int32) .The Take<TSource>(IQueryable<TSource>, Int32) method generates a MethodCallExpression that represents calling Take<TSource>(IQueryable<TSource>, Int32) itself as a constructed generic method. 그런 다음 MethodCallExpression CreateQuery(Expression) IQueryProvider Provider 매개 변수의 속성이 나타내는의 메서드에를 전달 합니다 source .It then passes the MethodCallExpression to the CreateQuery(Expression) method of the IQueryProvider represented by the Provider property of the source parameter.

호출을 나타내는 식 트리를 실행 한 결과로 발생 하는 쿼리 동작은 Take<TSource>(IQueryable<TSource>, Int32) 매개 변수의 형식 구현에 따라 달라 집니다 source .The query behavior that occurs as a result of executing an expression tree that represents calling Take<TSource>(IQueryable<TSource>, Int32) depends on the implementation of the type of the source parameter. 예상 되는 동작은 처음부터 시작 되는 첫 번째 요소를 사용 하는 것입니다 count source .The expected behavior is that it takes the first count elements from the start of source.

적용 대상