Enumerable.Take 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
多載
| 名稱 | Description |
|---|---|
| Take<TSource>(IEnumerable<TSource>, Int32) |
從序列開頭傳回指定的連續項目數目。 |
| Take<TSource>(IEnumerable<TSource>, Range) |
傳回序列中連續專案的指定範圍。 |
Take<TSource>(IEnumerable<TSource>, Int32)
- 來源:
- Take.cs
- 來源:
- Take.cs
- 來源:
- Take.cs
- 來源:
- Take.cs
- 來源:
- Take.cs
從序列開頭傳回指定的連續項目數目。
public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
static System::Collections::Generic::IEnumerable<TSource> ^ Take(System::Collections::Generic::IEnumerable<TSource> ^ source, int count);
public static System.Collections.Generic.IEnumerable<TSource> Take<TSource>(this System.Collections.Generic.IEnumerable<TSource> source, int count);
static member Take : seq<'Source> * int -> seq<'Source>
<Extension()>
Public Function Take(Of TSource) (source As IEnumerable(Of TSource), count As Integer) As IEnumerable(Of TSource)
類型參數
- TSource
元素 source的類型。
參數
- source
- IEnumerable<TSource>
要從中回傳元素的序列。
- count
- Int32
要回傳的元素數量。
傳回
IEnumerable<T>一個包含輸入序列起始點指定元素數量的 。
例外狀況
source 是 null。
範例
以下程式碼範例示範如何從 Take 序列開始時(排序後)回傳元素。
int[] grades = { 59, 82, 70, 56, 92, 98, 85 };
IEnumerable<int> topThreeGrades =
grades.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
*/
' Create an array of Integer values that represent grades.
Dim grades() As Integer = {59, 82, 70, 56, 92, 98, 85}
' Get the highest three grades by first sorting
' them in descending order and then taking the
' first three values.
Dim topThreeGrades As IEnumerable(Of Integer) =
grades _
.OrderByDescending(Function(grade) grade) _
.Take(3)
' Display the results.
Dim output As New System.Text.StringBuilder("The top three grades are:" & vbCrLf)
For Each grade As Integer In topThreeGrades
output.AppendLine(grade)
Next
Console.WriteLine(output.ToString())
' This code produces the following output:
'
' The top three grades are:
' 98
' 92
' 85
備註
此方法透過延遲執行實現。 立即回傳值是一個物件,儲存執行動作所需的所有資訊。 此方法所代表的查詢,直到物件被枚舉,無論是直接呼叫其 GetEnumerator 方法,或是使用 C# 中的 foreach,或在 Visual Basic 中使用 For Each,才會執行。
Take 枚 source 舉並產生元素,直到 count 元素被消除或 source 不再包含其他元素為止。 若 count 超過 中 source元素數,則返回所有元素 source 。
若 count 小於或等於零,則 source 不列舉,回傳空 IEnumerable<T> 值。
與TakeSkip方法是功能互補。 給定一個集合序列coll和一個整數n,將 和 的結果coll.Take(n)coll.Skip(n)串接起來,得到與 相同的序列。coll
在Visual Basic查詢表達式語法中,Take子句可轉譯為Take的呼叫。
另請參閱
適用於
Take<TSource>(IEnumerable<TSource>, Range)
- 來源:
- Take.cs
- 來源:
- Take.cs
- 來源:
- Take.cs
- 來源:
- Take.cs
- 來源:
- Take.cs
傳回序列中連續專案的指定範圍。
public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
static System::Collections::Generic::IEnumerable<TSource> ^ Take(System::Collections::Generic::IEnumerable<TSource> ^ source, Range range);
public static System.Collections.Generic.IEnumerable<TSource> Take<TSource>(this System.Collections.Generic.IEnumerable<TSource> source, Range range);
static member Take : seq<'Source> * Range -> seq<'Source>
<Extension()>
Public Function Take(Of TSource) (source As IEnumerable(Of TSource), range As Range) As IEnumerable(Of TSource)
類型參數
- TSource
元素 source的類型。
參數
- source
- IEnumerable<TSource>
要從中回傳元素的序列。
- range
- Range
要回傳的元素範圍,起始與結束的索引可從序列的開始或結束處開始。
傳回
一個 IEnumerable<T> 包含序列中指定元素範圍 source 的 。
例外狀況
source 是 null。
備註
此方法透過延遲執行實現。 立即回傳值是一個物件,儲存執行動作所需的所有資訊。 此方法所代表的查詢,直到物件被枚舉,無論是直接呼叫其 GetEnumerator 方法,或是使用 C# 中的 foreach,或在 Visual Basic 中使用 For Each,才會執行。
Take 枚 source 舉並產生索引屬於指定 range元素的元素。