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 # 中使用或 For Each 在 Visual Basic 中使用。The 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 # 中使用或 For Each 在 Visual Basic 中使用。The 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.

另请参阅

适用于