Enumerable.Take(IEnumerable<TSource>, Int32) Enumerable.Take(IEnumerable<TSource>, Int32) Enumerable.Take(IEnumerable<TSource>, Int32) Enumerable.Take(IEnumerable<TSource>, Int32) Method

定義

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

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

要素の型ソースします。 The type of the elements of source.

パラメーター
source
IEnumerable<TSource>

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

count
Int32 Int32 Int32 Int32

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

戻り値
IEnumerable<TSource>

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

例外

次のコード例は、使用する方法を示します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)
{
    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
MsgBox(output.ToString())

' 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.

場合countがゼロ未満source列挙され、空でないIEnumerable<T>が返されます。If count is less than or equal to zero, source is not enumerated and an empty IEnumerable<T> is returned.

TakeSkipメソッドは、機能を補完します。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.

適用対象

こちらもご覧ください