Enumerable.TakeWhile 方法

定義

只要指定的條件為 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>)

只要指定的條件為 true,就會傳回序列中的項目。Returns elements from a sequence as long as a specified condition is true.

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>)

只要指定的條件為 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
Console.WriteLine(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 foreach Visual c # 或 Visual Basic 中的方法來列舉物件為止 For EachThe 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>)

只要指定的條件為 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
Console.WriteLine(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 foreach Visual c # 或 Visual Basic 中的方法來列舉物件為止 For EachThe 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.

適用於