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

Source:
Take.cs
Source:
Take.cs
Source:
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# For Eachforeach 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>)

Source:
Take.cs
Source:
Take.cs
Source:
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# For Eachforeach 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