Enumerable.Take メソッド

定義

オーバーロード

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

シーケンスの先頭から、指定された数の連続する要素を返します。

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

シーケンスから指定した連続する要素の範囲を返します。

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

シーケンスの先頭から、指定された数の連続する要素を返します。

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<TSource>

入力シーケンスの先頭から、指定された数の要素を含む IEnumerable<T>

例外

sourcenullです。

次のコード例では、 を使用 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# または For Each Visual Basic で を使用foreachして列挙されるまで実行されません。

Take 要素が source 生成されるか、それ以上要素が含まれないまで count 、要素を列挙して source 生成します。 countが 内sourceの要素の数を超えると、 のすべての要素sourceが返されます。

が 0 以下の場合 countsource は列挙されず、空 IEnumerable<T> が返されます。

Takeメソッドと Skip メソッドは機能補完です。 コレクション シーケンス coll と整数 nを指定すると、 の結果 coll.Take(n) を連結すると、 と coll.Skip(n) 同じシーケンス collが生成されます。

Visual Basic クエリ式の構文では、 句は TakeTake呼び出しに変換されます。

こちらもご覧ください

適用対象

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

シーケンスから指定した連続する要素の範囲を返します。

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<TSource>

IEnumerable<T>シーケンスの指定された要素範囲を格納する source

例外

sourcenull です。

注釈

このメソッドは、遅延実行を使用して実装されます。 即時戻り値は、アクションの実行に必要なすべての情報を格納する オブジェクトです。 このメソッドで表されるクエリは、オブジェクトがメソッドを直接呼び出GetEnumeratorすか、C# または For Each Visual Basic で を使用foreachして列挙されるまで実行されません。

Take は、 source 指定した に属するインデックスを持つ要素を列挙して生成します range

適用対象