Queryable.Take<TSource>(IQueryable<TSource>, Int32) 方法
定義
從序列開頭傳回指定的連續項目數目。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)
類型參數
- TSource
source
項目的類型。The type of the elements of source
.
參數
- source
- IQueryable<TSource>
傳回項目的序列。The sequence to return elements from.
- count
- Int32
要傳回的項目數目。The number of elements to return.
傳回
- IQueryable<TSource>
IQueryable<T>,其中包含來自 source
開頭的指定項目數目。An IQueryable<T> that contains the specified number of elements from the start of source
.
例外狀況
source
為 null
。source
is null
.
範例
下列程式碼範例示範如何使用 Take<TSource>(IQueryable<TSource>, Int32) ,從序列的開頭傳回元素。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
備註
Take<TSource>(IQueryable<TSource>, Int32)方法 MethodCallExpression 會產生,代表呼叫 Take<TSource>(IQueryable<TSource>, Int32) 本身做為已建立的泛型方法。The Take<TSource>(IQueryable<TSource>, Int32) method generates a MethodCallExpression that represents calling Take<TSource>(IQueryable<TSource>, Int32) itself as a constructed generic method. 然後,它會將傳遞 MethodCallExpression 給以 CreateQuery(Expression) 參數的屬性所表示的方法 IQueryProvider Provider source
。It then passes the MethodCallExpression to the CreateQuery(Expression) method of the IQueryProvider represented by the Provider property of the source
parameter.
執行表示呼叫的運算式樹狀架構所產生的查詢行為, Take<TSource>(IQueryable<TSource>, Int32) 取決於參數類型的實作為 source
。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. 預期的行為是它會採用開頭的第一個 count
元素 source
。The expected behavior is that it takes the first count
elements from the start of source
.