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>

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

Eccezioni

Esempi

Nell'esempio di codice riportato di seguito viene Take<TSource>(IQueryable<TSource>, Int32) illustrato come utilizzare per restituire 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 oggetto che rappresenta Take<TSource>(IQueryable<TSource>, Int32) la chiamata a se stesso come 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 MethodCallExpression IQueryProvider quindi al source metodo dell'oggetto rappresentato dalla proprietàdelparametro.Provider CreateQuery(Expression)It then passes the MethodCallExpression to the CreateQuery(Expression) method of the IQueryProvider represented by the Provider property of the source parameter.

Il comportamento della query che si verifica in seguito all'esecuzione di un albero delle espressioni che Take<TSource>(IQueryable<TSource>, Int32) rappresenta la chiamata dipende dall'implementazione del tipo source di 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 prende i primi count elementi dall'inizio di. sourceThe expected behavior is that it takes the first count elements from the start of source.

Si applica a