Enumerable.Take メソッド

定義

オーバーロード

名前 説明
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<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# で foreach を使用するか、Visual Basic で For Each を使用して列挙されるまで実行されません。

Take sourceを列挙し、count要素が生成されるか、sourceに要素が含まれるまで要素を生成します。 countsource内の要素の数を超える場合は、sourceのすべての要素が返されます。

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

TakeメソッドとSkipメソッドは、関数型補数です。 コレクション シーケンス 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<TSource>

source シーケンスの指定した範囲の要素を含むIEnumerable<T>

例外

sourcenullです。

注釈

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

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

適用対象