Enumerable.Take<TSource>(IEnumerable<TSource>, Int32) メソッド


シーケンスの先頭から、指定された数の連続する要素を返します。Returns a specified number of contiguous elements from the start of a sequence.

generic <typename TSource>
 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>
Public Function Take(Of TSource) (source As IEnumerable(Of TSource), count As Integer) As IEnumerable(Of TSource)



source の要素の型。The type of the elements of source.



要素を返すシーケンス。The sequence to return elements from.


返す要素数。The number of elements to return.



入力シーケンスの先頭から、指定された数の要素を含む IEnumerable<T>An IEnumerable<T> that contains the specified number of elements from the start of the input sequence.


sourcenullです。source is null.

次のコード例は、を使用し Take て、シーケンスの先頭から要素を返す方法を示しています。The following code example demonstrates how to use Take to return elements from the start of a sequence.

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)
 This code produces the following output:

 The top three grades are:
' 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) _

' Display the results.
Dim output As New System.Text.StringBuilder("The top three grades are:" & vbCrLf)
For Each grade As Integer In topThreeGrades

' This code produces the following output:
' The top three grades are:
' 98
' 92
' 85


このメソッドは、遅延実行を使用して実装されます。This method is implemented by using deferred execution. イミディエイトの戻り値は、アクションを実行するために必要なすべての情報を格納するオブジェクトです。The immediate return value is an object that stores all the information that is required to perform the action. か、呼び出すことによって、オブジェクトが列挙されるまで、このメソッドによって表されるクエリは実行されません、GetEnumeratorメソッドを使用して直接またはforeachVisual C# またはFor EachVisual Basic で。The query represented by this method is not executed until the object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or For Each in Visual Basic.

Take 要素 source count が取得されるか、またはそれ以上の要素が含まれるまで、要素を列挙して生成し source ます。Take enumerates source and yields elements until count elements have been yielded or source contains no more elements. count 内の要素の数を超えると source 、のすべての要素 source が返されます。If count exceeds the number of elements in source, all elements of source are returned.

が0以下の場合 countsource は列挙されず、空のが IEnumerable<T> 返されます。If count is less than or equal to zero, source is not enumerated and an empty IEnumerable<T> is returned.

Takeメソッドと Skip メソッドは、機能的に補完されます。The Take and Skip methods are functional complements. シーケンスと整数を指定した coll n 場合、との結果を連結すると、と coll.Take(n) coll.Skip(n) 同じシーケンスが生成され coll ます。Given a sequence coll and an integer n, concatenating the results of coll.Take(n) and coll.Skip(n) yields the same sequence as coll.

Visual Basic のクエリ式の構文では、 Take 句はの呼び出しに変換 Take します。In Visual Basic query expression syntax, a Take clause translates to an invocation of Take.