Queryable.Take 方法

定義

多載

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

從序列開頭傳回指定的連續項目數目。

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

從序列傳回指定的連續專案範圍。

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

來源:
Queryable.cs
來源:
Queryable.cs
來源:
Queryable.cs

從序列開頭傳回指定的連續項目數目。

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 項目的類型。

參數

source
IQueryable<TSource>

傳回項目的序列。

count
Int32

要傳回的項目數目。

傳回

IQueryable<TSource>

IQueryable<T>,其中包含來自 source 開頭的指定項目數目。

例外狀況

sourcenull

範例

下列程式碼範例示範如何使用 Take<TSource>(IQueryable<TSource>, Int32) 從序列開頭傳回專案。

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) 本身為建構的泛型方法。 然後,它會將 傳遞給 MethodCallExpressionCreateQuery(Expression) 參數之 屬性所 Provider 表示的 方法 IQueryProvidersource

執行表示呼叫 Take<TSource>(IQueryable<TSource>, Int32) 的運算式樹狀結構所產生的查詢行為,取決於參數類型的實作 source 。 預期的行為是它會從 開頭 source 取得第一個專案 count

適用於

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

來源:
Queryable.cs
來源:
Queryable.cs
來源:
Queryable.cs

從序列傳回指定的連續專案範圍。

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)

類型參數

TSource

source 項目的類型。

參數

source
IQueryable<TSource>

傳回項目的序列。

range
Range

要傳回的專案範圍,其開頭或結尾都有開始和結束索引。

傳回

IQueryable<TSource>

IQueryable<T> 包含序列中 source 指定 range 的專案。

例外狀況

sourcenull

適用於