Enumerable.TakeWhile メソッド

定義

指定された条件を満たされる限り、シーケンスから要素を返した後、残りの要素をスキップします。

オーバーロード

TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,Int32,Boolean>)

指定された条件が満たされる限り、シーケンスから要素を返します。 要素のインデックスは、述語関数のロジックで使用されます。

TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)

指定された条件が満たされる限り、シーケンスから要素を返します。

TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,Int32,Boolean>)

Source:
Take.cs
Source:
Take.cs
Source:
Take.cs

指定された条件が満たされる限り、シーケンスから要素を返します。 要素のインデックスは、述語関数のロジックで使用されます。

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static System::Collections::Generic::IEnumerable<TSource> ^ TakeWhile(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, int, bool> ^ predicate);
public static System.Collections.Generic.IEnumerable<TSource> TakeWhile<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,int,bool> predicate);
static member TakeWhile : seq<'Source> * Func<'Source, int, bool> -> seq<'Source>
<Extension()>
Public Function TakeWhile(Of TSource) (source As IEnumerable(Of TSource), predicate As Func(Of TSource, Integer, Boolean)) As IEnumerable(Of TSource)

型パラメーター

TSource

source の要素の型。

パラメーター

source
IEnumerable<TSource>

要素を返すシーケンス。

predicate
Func<TSource,Int32,Boolean>

各ソース要素が条件に当てはまるかどうかをテストする関数。この関数の 2 つ目のパラメーターは、ソース要素のインデックスを表します。

戻り値

IEnumerable<TSource>

テストに合格しなくなった要素の前に出現する、入力シーケンスの要素を含む IEnumerable<T>

例外

source または predicatenull です。

次のコード例では、 を使用して、要素のインデックスを使用 TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,Int32,Boolean>) する条件が true である限り、シーケンスの先頭から要素を返す方法を示します。

string[] fruits = { "apple", "passionfruit", "banana", "mango",
                      "orange", "blueberry", "grape", "strawberry" };

IEnumerable<string> query =
    fruits.TakeWhile((fruit, index) => fruit.Length >= index);

foreach (string fruit in query)
{
    Console.WriteLine(fruit);
}

/*
 This code produces the following output:

 apple
 passionfruit
 banana
 mango
 orange
 blueberry
*/
' Create an array of strings.
Dim fruits() As String =
{"apple", "passionfruit", "banana", "mango",
 "orange", "blueberry", "grape", "strawberry"}

' Take strings from the array until one
' of the string's lengths is greater than or
' equal to the string item's index in the array.
Dim query As IEnumerable(Of String) =
fruits.TakeWhile(Function(fruit, index) _
                     fruit.Length >= index)

' Display the results.
Dim output As New System.Text.StringBuilder
For Each fruit As String In query
    output.AppendLine(fruit)
Next
Console.WriteLine(output.ToString())

' This code produces the following output:
'
' apple
' passionfruit
' banana
' mango
' orange
' blueberry

注釈

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

メソッドは TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,Int32,Boolean>) を使用して の source 各要素を predicate テストし、結果が の場合は true要素を生成します。 列挙体は、述語関数が 要素に対して false を返すか、それ以上要素が含まれない場合 source に停止します。

の最初の predicate 引数は、テストする要素を表します。 2 番目の引数は、 内 sourceの要素の 0 から始まるインデックスを表します。

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

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

こちらもご覧ください

適用対象

TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)

Source:
Take.cs
Source:
Take.cs
Source:
Take.cs

指定された条件が満たされる限り、シーケンスから要素を返します。

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static System::Collections::Generic::IEnumerable<TSource> ^ TakeWhile(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, bool> ^ predicate);
public static System.Collections.Generic.IEnumerable<TSource> TakeWhile<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,bool> predicate);
static member TakeWhile : seq<'Source> * Func<'Source, bool> -> seq<'Source>
<Extension()>
Public Function TakeWhile(Of TSource) (source As IEnumerable(Of TSource), predicate As Func(Of TSource, Boolean)) As IEnumerable(Of TSource)

型パラメーター

TSource

source の要素の型。

パラメーター

source
IEnumerable<TSource>

要素を返すシーケンス。

predicate
Func<TSource,Boolean>

各要素が条件を満たしているかどうかをテストする関数。

戻り値

IEnumerable<TSource>

テストに合格しなくなった要素の前に出現する、入力シーケンスの要素を含む IEnumerable<T>

例外

source または predicatenull です。

次のコード例では、 を使用 TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) して、条件が true である限り、シーケンスの先頭から要素を返す方法を示します。

string[] fruits = { "apple", "banana", "mango", "orange",
                      "passionfruit", "grape" };

IEnumerable<string> query =
    fruits.TakeWhile(fruit => String.Compare("orange", fruit, true) != 0);

foreach (string fruit in query)
{
    Console.WriteLine(fruit);
}

/*
 This code produces the following output:

 apple
 banana
 mango
*/
' Create an array of strings.
Dim fruits() As String =
{"apple", "banana", "mango", "orange", "passionfruit", "grape"}

' Take strings from the array until one of
' the strings matches "orange".
Dim query As IEnumerable(Of String) =
fruits.TakeWhile(Function(fruit) _
                     String.Compare("orange", fruit, True) <> 0)

' Display the results.
Dim output As New System.Text.StringBuilder
For Each fruit As String In query
    output.AppendLine(fruit)
Next
Console.WriteLine(output.ToString())

' This code produces the following output:
'
' apple
' banana
' mango

注釈

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

メソッドは TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) を使用して の source 各要素を predicate テストし、結果が の場合は true要素を生成します。 列挙体は、述語関数が 要素に対して false を返すか、それ以上要素が含まれない場合 source に停止します。

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

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

こちらもご覧ください

適用対象