共用方式為


Queryable.Take 方法

定義

多載

名稱 Description
Take<TSource>(IQueryable<TSource>, Int32)

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

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

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

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

來源:
Queryable.cs
來源:
Queryable.cs
來源:
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);
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")]
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>
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")>]
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)IQueryProvider由參數Provider性質source所表示的方法。

執行代表呼叫 Take<TSource>(IQueryable<TSource>, Int32) 的表達式樹所產生的查詢行為,取決於參數型別 source 的實作。 預期行為是從 的source起始點取出第一個count元素。

適用於

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

來源:
Queryable.cs
來源:
Queryable.cs
來源:
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);
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")]
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>
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")>]
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

適用於