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

Definizione

Restituisce un numero specificato di elementi contigui dall'inizio di una sequenza.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)

Parametri di tipo

TSource

Tipo degli elementi di source.The type of the elements of source.

Parametri

source
IQueryable<TSource>

Sequenza dalla quale vengono restituiti gli elementi.The sequence to return elements from.

count
Int32 Int32 Int32 Int32

Numero di elementi da restituire.The number of elements to return.

Restituisce

IQueryable<TSource>

Un' IQueryable<T> che contiene il numero specificato di elementi dall'inizio del source.An IQueryable<T> that contains the specified number of elements from the start of source.

Eccezioni

Esempi

Esempio di codice seguente viene illustrato come utilizzare Take<TSource>(IQueryable<TSource>, Int32) per restituire gli elementi dall'inizio di una sequenza.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

Commenti

Il Take<TSource>(IQueryable<TSource>, Int32) metodo genera un MethodCallExpression che rappresenta una chiamata Take<TSource>(IQueryable<TSource>, Int32) stesso come un metodo generico costruito.The Take<TSource>(IQueryable<TSource>, Int32) method generates a MethodCallExpression that represents calling Take<TSource>(IQueryable<TSource>, Int32) itself as a constructed generic method. Passa quindi il MethodCallExpression per il CreateQuery(Expression) metodo del IQueryProvider rappresentato dal Provider proprietà del source parametro.It then passes the MethodCallExpression to the CreateQuery(Expression) method of the IQueryProvider represented by the Provider property of the source parameter.

Il comportamento delle query che si verifica in seguito all'esecuzione di un albero delle espressioni che rappresenta una chiamata Take<TSource>(IQueryable<TSource>, Int32) dipende dall'implementazione del tipo del source parametro.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. Il comportamento previsto è che impiega il primo count gli elementi dall'inizio del source.The expected behavior is that it takes the first count elements from the start of source.

Si applica a