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

來源:
Take.cs
來源:
Take.cs
來源:
Take.cs

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

C#
public static System.Collections.Generic.IEnumerable<TSource> TakeWhile<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,int,bool> predicate);

類型參數

TSource

source 項目的類型。

參數

source
IEnumerable<TSource>

傳回項目的序列。

predicate
Func<TSource,Int32,Boolean>

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

傳回

IEnumerable<TSource>

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

例外狀況

sourcepredicatenull

範例

下列程式代碼範例示範如何使用 TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,Int32,Boolean>) 傳回序列開頭的專案,只要使用元素的索引的條件為 true。

C#
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
*/

備註

這個方法是使用延後執行來實作。 立即傳回值是一個物件,會儲存執行動作所需的所有資訊。 除非直接呼叫其 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調用。

另請參閱

適用於

.NET 9 和其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

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

來源:
Take.cs
來源:
Take.cs
來源:
Take.cs

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

C#
public static System.Collections.Generic.IEnumerable<TSource> TakeWhile<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,bool> predicate);

類型參數

TSource

source 項目的類型。

參數

source
IEnumerable<TSource>

傳回項目的序列。

predicate
Func<TSource,Boolean>

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

傳回

IEnumerable<TSource>

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

例外狀況

sourcepredicatenull

範例

下列程式代碼範例示範如何使用 TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) 傳回序列開頭的專案,只要條件為 true。

C#
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
*/

備註

這個方法是使用延後執行來實作。 立即傳回值是一個物件,會儲存執行動作所需的所有資訊。 除非直接呼叫其 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調用。

另請參閱

適用於

.NET 9 和其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0