Enumerable.TakeWhile メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
指定された条件を満たされる限り、シーケンスから要素を返した後、残りの要素をスキップします。
オーバーロード
TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,Int32,Boolean>) |
指定された条件が満たされる限り、シーケンスから要素を返します。 要素のインデックスは、述語関数のロジックで使用されます。 |
TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) |
指定された条件が満たされる限り、シーケンスから要素を返します。 |
TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,Int32,Boolean>)
指定された条件が満たされる限り、シーケンスから要素を返します。 要素のインデックスは、述語関数のロジックで使用されます。
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>
要素を返すシーケンス。
各ソース要素が条件に当てはまるかどうかをテストする関数。この関数の 2 つ目のパラメーターは、ソース要素のインデックスを表します。
戻り値
- IEnumerable<TSource>
テストに合格しなくなった要素の前に出現する、入力シーケンスの要素を含む IEnumerable<T>。
例外
source
または predicate
が null
です。
例
次のコード例では、要素のインデックスを使用 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
メソッドを使用して直接またはforeach
Visual C# またはFor Each
Visual Basic で。
メソッドはTakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,Int32,Boolean>)、使用して各要素をpredicate``source
テストし、結果true
が . 列挙は、述語関数が要素に false
対して返されたとき、またはそれ以上要素が含まれていると source
き、停止します。
最初の predicate
引数は、テストする要素を表します。 2 番目の引数は、内 source
の要素の 0 から始まるインデックスを表します。
メソッド TakeWhile と SkipWhile メソッドは、機能的な補数です。 コレクション シーケンス coll
と純粋関数 p
を指定すると、結果 coll.TakeWhile(p)
が連結され coll.SkipWhile(p)
、次と coll
同じシーケンスが生成されます。
Visual Basicクエリ式の構文では、句は Take While
TakeWhile.
こちらもご覧ください
適用対象
TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)
指定された条件が満たされる限り、シーケンスから要素を返します。
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>
要素を返すシーケンス。
戻り値
- IEnumerable<TSource>
テストに合格しなくなった要素の前に出現する、入力シーケンスの要素を含む IEnumerable<T>。
例外
source
または predicate
が null
です。
例
次のコード例は、条件が true である限り、シーケンスの先頭から要素を返すために使用 TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) する方法を示しています。
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
メソッドを使用して直接またはforeach
Visual C# またはFor Each
Visual Basic で。
メソッドはTakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)、使用して各要素をpredicate``source
テストし、結果true
が . 列挙は、述語関数が要素に false
対して返されたとき、またはそれ以上要素が含まれていると source
き、停止します。
メソッド TakeWhile と SkipWhile メソッドは、機能的な補数です。 コレクション シーケンス coll
と純粋関数 p
を指定すると、結果 coll.TakeWhile(p)
が連結され coll.SkipWhile(p)
、次と coll
同じシーケンスが生成されます。
Visual Basicクエリ式の構文では、句は Take While
TakeWhile.