Enumerable.TakeWhile Enumerable.TakeWhile Enumerable.TakeWhile Enumerable.TakeWhile Method

定義

指定された条件を満たされる限り、シーケンスから要素を返した後、残りの要素をスキップします。Returns elements from a sequence as long as a specified condition is true, and then skips the remaining elements.

オーバーロード

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

指定された条件が満たされる限り、シーケンスから要素を返します。Returns elements from a sequence as long as a specified condition is true.

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

指定された条件が満たされる限り、シーケンスから要素を返します。Returns elements from a sequence as long as a specified condition is true. 要素のインデックスは、述語関数のロジックで使用されます。The element's index is used in the logic of the predicate function.

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

指定された条件が満たされる限り、シーケンスから要素を返します。Returns elements from a sequence as long as a specified condition is true.

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 の要素の型。The type of the elements of source.

パラメーター

source
IEnumerable<TSource>

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

predicate
Func<TSource,Boolean>

各要素が条件を満たしているかどうかをテストする関数。A function to test each element for a condition.

戻り値

IEnumerable<TSource>

テストに合格しなくなった要素の前に出現する、入力シーケンスの要素を含む IEnumerable<T>An IEnumerable<T> that contains the elements from the input sequence that occur before the element at which the test no longer passes.

例外

source または predicatenull です。source or predicate is null.

次のコード例は、使用する方法を示しますTakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)を条件が true であれば、シーケンスの先頭から要素を返します。The following code example demonstrates how to use TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) to return elements from the start of a sequence as long as a condition is 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
MsgBox(output.ToString())

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

注釈

このメソッドは、遅延実行を使用して実装されます。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.

TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)メソッドの各要素はテストsourceを使用してpredicateし、結果がある場合、要素を生成trueします。The TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) method tests each element of source by using predicate and yields the element if the result is true. 述語関数で返されるときに、列挙が停止するfalse要素の場合、またはsourceない多くの要素が含まれています。Enumeration stops when the predicate function returns false for an element or when source contains no more elements.

TakeWhileSkipWhileメソッドは、機能を補完します。The TakeWhile and SkipWhile methods are functional complements. シーケンスの指定collと純粋関数pの結果を連結coll.TakeWhile(p)coll.SkipWhile(p)と同じシーケンスを生成collGiven a sequence coll and a pure function p, concatenating the results of coll.TakeWhile(p) and coll.SkipWhile(p) yields the same sequence as coll.

Visual Basic のクエリ式の構文をTake While句がの呼び出しに変換されますTakeWhileします。In Visual Basic query expression syntax, a Take While clause translates to an invocation of TakeWhile.

こちらもご覧ください

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

指定された条件が満たされる限り、シーケンスから要素を返します。Returns elements from a sequence as long as a specified condition is true. 要素のインデックスは、述語関数のロジックで使用されます。The element's index is used in the logic of the predicate function.

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 の要素の型。The type of the elements of source.

パラメーター

source
IEnumerable<TSource>

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

predicate
Func<TSource,Int32,Boolean>

各ソース要素が条件に当てはまるかどうかをテストする関数。この関数の 2 つ目のパラメーターは、ソース要素のインデックスを表します。A function to test each source element for a condition; the second parameter of the function represents the index of the source element.

戻り値

IEnumerable<TSource>

テストに合格しなくなった要素の前に出現する、入力シーケンスの要素を含む IEnumerable<T>An IEnumerable<T> that contains elements from the input sequence that occur before the element at which the test no longer passes.

例外

source または predicatenull です。source or predicate is null.

次のコード例は、使用する方法を示しますTakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,Int32,Boolean>)を要素のインデックスを使用する条件が真である限り、シーケンスの先頭から要素を返します。The following code example demonstrates how to use TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,Int32,Boolean>) to return elements from the start of a sequence as long as a condition that uses the element's index is 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
MsgBox(output.ToString())

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

注釈

このメソッドは、遅延実行を使用して実装されます。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.

TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,Int32,Boolean>)メソッドの各要素はテストsourceを使用してpredicateし、結果がある場合、要素を生成trueします。The TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,Int32,Boolean>) method tests each element of source by using predicate and yields the element if the result is true. 述語関数で返されるときに、列挙が停止するfalse要素の場合、またはsourceない多くの要素が含まれています。Enumeration stops when the predicate function returns false for an element or when source contains no more elements.

最初の引数predicateをテストする要素を表します。The first argument of predicate represents the element to test. 2 番目の引数内の要素の 0 から始まるインデックスを表すsourceします。The second argument represents the zero-based index of the element within source.

TakeWhileSkipWhileメソッドは、機能を補完します。The TakeWhile and SkipWhile methods are functional complements. シーケンスの指定collと純粋関数pの結果を連結coll.TakeWhile(p)coll.SkipWhile(p)と同じシーケンスを生成collGiven a sequence coll and a pure function p, concatenating the results of coll.TakeWhile(p) and coll.SkipWhile(p) yields the same sequence as coll.

Visual Basic のクエリ式の構文をTake While句がの呼び出しに変換されますTakeWhileします。In Visual Basic query expression syntax, a Take While clause translates to an invocation of TakeWhile.

こちらもご覧ください

適用対象