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方法直接或透過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,並產生項目,如果結果是trueThe 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子句會轉譯成的引動過程TakeWhileIn 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方法直接或透過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,並產生項目,如果結果是trueThe 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. 第二個引數所代表的項目內的以零起始的索引sourceThe 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子句會轉譯成的引動過程TakeWhileIn Visual Basic query expression syntax, a Take While clause translates to an invocation of TakeWhile.

另請參閱

適用於