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# For Eachforeach Visual Basic 中使用 来枚举对象之前,不会执行此方法表示的查询。

方法TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,Int32,Boolean>)使用 predicate 测试 的每个source元素,如果结果为 true,则生成 元素。 当谓词函数 false 返回元素或不包含更多元素时 source ,枚举将停止。

的第一个参数 predicate 表示要测试的元素。 第二个参数表示 中 source元素的从零开始的索引。

TakeWhileSkipWhile 方法是功能补充。 给定一个集合序列coll和一个纯函数p,将 和 coll.SkipWhile(p) 的结果coll.TakeWhile(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# For Eachforeach Visual Basic 中使用 来枚举对象之前,不会执行此方法表示的查询。

方法TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)使用 predicate 测试 的每个source元素,如果结果为 true,则生成 元素。 当谓词函数 false 返回元素或不包含更多元素时 source ,枚举将停止。

TakeWhileSkipWhile 方法是功能补充。 给定一个集合序列coll和一个纯函数p,将 和 coll.SkipWhile(p) 的结果coll.TakeWhile(p)串联在一起,生成与 相同的序列coll

在 Visual Basic 查询表达式语法中, Take While 子句转换为 的 TakeWhile调用。

另请参阅

适用于