Queryable.Take(IQueryable<TSource>, Int32) Queryable.Take(IQueryable<TSource>, Int32) Queryable.Take(IQueryable<TSource>, Int32) Queryable.Take(IQueryable<TSource>, Int32) Method

定義

從序列開頭傳回指定的連續項目數目。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 Int32 Int32 Int32

要傳回的項目數目。The number of elements to return.

傳回

IQueryable<TSource>

IQueryable<T>,其中包含來自 source 開頭的指定項目數目。An IQueryable<T> that contains the specified number of elements from the start of source.

例外狀況

範例

下列程式碼範例示範如何使用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)至由Provider source參數IQueryProvider的屬性所表示之的方法。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.

適用於