Queryable.Take Metoda

Definicja

Przeciążenia

Take<TSource>(IQueryable<TSource>, Int32)

Zwraca określoną liczbę ciągłych elementów od początku sekwencji.

Take<TSource>(IQueryable<TSource>, Range)

Zwraca określony zakres ciągłych elementów z sekwencji.

Take<TSource>(IQueryable<TSource>, Int32)

Źródło:
Queryable.cs
Źródło:
Queryable.cs
Źródło:
Queryable.cs

Zwraca określoną liczbę ciągłych elementów od początku sekwencji.

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)

Parametry typu

TSource

Typ elementów elementu source.

Parametry

source
IQueryable<TSource>

Sekwencja zwracania elementów z.

count
Int32

Liczba elementów do zwrócenia.

Zwraca

IQueryable<TSource>

Element IQueryable<T> zawierający określoną liczbę elementów od początku .source

Wyjątki

source to null.

Przykłady

W poniższym przykładzie kodu pokazano, jak używać Take<TSource>(IQueryable<TSource>, Int32) funkcji zwracania elementów od początku sekwencji.

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

Uwagi

Metoda Take<TSource>(IQueryable<TSource>, Int32) generuje element MethodCallExpression , który reprezentuje wywołanie Take<TSource>(IQueryable<TSource>, Int32) siebie jako skonstruowaną metodę ogólną. Następnie przekazuje MethodCallExpression element do CreateQuery(Expression) metody reprezentowanej IQueryProvider przez Provider właściwość parametru source .

Zachowanie zapytania, które występuje w wyniku wykonania drzewa wyrażeń, które reprezentuje wywołanie Take<TSource>(IQueryable<TSource>, Int32) , zależy od implementacji typu parametru source . Oczekiwane zachowanie polega na tym, że pierwsze count elementy są pobierane od początku .source

Dotyczy

Take<TSource>(IQueryable<TSource>, Range)

Źródło:
Queryable.cs
Źródło:
Queryable.cs
Źródło:
Queryable.cs

Zwraca określony zakres ciągłych elementów z sekwencji.

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

Parametry typu

TSource

Typ elementów elementu source.

Parametry

source
IQueryable<TSource>

Sekwencja zwracania elementów z.

range
Range

Zakres elementów do zwrócenia, który ma indeksy początkowe i końcowe od początku lub na końcu.

Zwraca

IQueryable<TSource>

Element IQueryable<T> zawierający określone range elementy z source sekwencji.

Wyjątki

source to null.

Dotyczy