Enumerable.TakeWhile 方法

定義

只要指定的條件為 true,就會傳回序列中的項目,然後略過其餘項目。

多載

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

只要指定的條件為 true,就會傳回序列中的項目。 項目的索引是用於述詞功能的邏輯中。

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

只要指定的條件為 true,就會傳回序列中的項目。

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

只要指定的條件為 true,就會傳回序列中的項目。 項目的索引是用於述詞功能的邏輯中。

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>

傳回項目的序列。

predicate
Func<TSource,Int32,Boolean>

用來測試各來源項目是否符合條件的函式;此函式的第二個參數代表來源項目的索引。

傳回

IEnumerable<TSource>

IEnumerable<T>,其中包含輸入序列中的項目,而這些項目出現在已無法通過測試的項目前面。

例外狀況

sourcepredicatenull

範例

下列程式碼範例示範如何使用 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 方法或在 C# 或 foreachFor Each Visual Basic 中使用 來列舉物件,否則不會執行這個方法所代表的查詢。

方法會 TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,Int32,Boolean>) 使用 predicate 來測試 的每個專案 source ,如果結果為 true ,則會產生 專案。 列舉會在述詞函式傳回 false 元素時停止,或當不包含其他元素時 source 停止。

的第一個引數 predicate 代表要測試的專案。 第二個引數代表 內 source 專案之以零起始的索引。

TakeWhileSkipWhile 方法是功能補充。 給定集合序列 coll 和純函 p 式 ,串連 和 的結果 coll.TakeWhile(p) 會產生 coll.SkipWhile(p) 與 相同的序列 coll

在 Visual Basic 查詢運算式語法中, Take While 子句會轉譯為 的 TakeWhile 調用。

另請參閱

適用於

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

只要指定的條件為 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 項目的類型。

參數

source
IEnumerable<TSource>

傳回項目的序列。

predicate
Func<TSource,Boolean>

用來測試每個項目是否符合條件的函式。

傳回

IEnumerable<TSource>

IEnumerable<T> 其中包含輸入序列中的項目,而這些項目出現在已無法通過測試的項目前面。

例外狀況

sourcepredicatenull

範例

下列程式碼範例示範如何使用 TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) 傳回序列開頭的專案,只要條件為 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

備註

這個方法是使用延後執行來實作。 立即傳回值是一個物件,會儲存執行動作所需的所有資訊。 除非直接呼叫其 GetEnumerator 方法或在 C# 或 foreachFor Each Visual Basic 中使用 來列舉物件,否則不會執行這個方法所代表的查詢。

方法會 TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) 使用 predicate 來測試 的每個專案 source ,如果結果為 true ,則會產生 專案。 列舉會在述詞函式傳回 false 元素時停止,或當不包含其他元素時 source 停止。

TakeWhileSkipWhile 方法是功能補充。 給定集合序列 coll 和純函 p 式 ,串連 和 的結果 coll.TakeWhile(p) 會產生 coll.SkipWhile(p) 與 相同的序列 coll

在 Visual Basic 查詢運算式語法中, Take While 子句會轉譯為 的 TakeWhile 調用。

另請參閱

適用於