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

定義

只要指定的條件為 true,就會傳回序列中的項目,然後略過其餘項目。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>)

只要指定的條件為 true,就會傳回序列中的項目。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>)

只要指定的條件為 true,就會傳回序列中的項目。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>)

只要指定的條件為 true,就會傳回序列中的項目。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.

例外狀況

sourcepredicatenullsource 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呼叫其方法, 或是在 Visual C#或For Each Visual Basic 中使用foreach來列舉物件之後, 才會執行。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.

方法會使用sourcepredicate測試的每個專案, 並在結果為true時產生元素。 TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)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, 串連的結果和coll.SkipWhile(p)產生與相同的順序。Given 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>)

只要指定的條件為 true,就會傳回序列中的項目。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>

用來測試各來源項目是否符合條件的函式;此函式的第二個參數代表來源項目的索引。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.

例外狀況

sourcepredicatenullsource or predicate is null.

範例

下列程式碼範例示範如何使用TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,Int32,Boolean>) , 只要使用專案索引的條件為 true, 就能從序列的開頭傳回專案。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呼叫其方法, 或是在 Visual C#或For Each Visual Basic 中使用foreach來列舉物件之後, 才會執行。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.

方法會使用sourcepredicate測試的每個專案, 並在結果為true時產生元素。 TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,Int32,Boolean>)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. 第二個引數代表內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, 串連的結果和coll.SkipWhile(p)產生與相同的順序。Given 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.

另請參閱

適用於