Queryable.TakeWhile 方法

定义

如果指定的条件为 true,则返回序列中的元素,然后跳过剩余的元素。

重载

TakeWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>)

只要指定的条件为 true,就会返回序列的元素。

TakeWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32,Boolean>>)

只要指定的条件为 true,就会返回序列的元素。 将在谓词函数的逻辑中使用元素的索引。

TakeWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>)

只要指定的条件为 true,就会返回序列的元素。

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static System::Linq::IQueryable<TSource> ^ TakeWhile(System::Linq::IQueryable<TSource> ^ source, System::Linq::Expressions::Expression<Func<TSource, bool> ^> ^ predicate);
public static System.Linq.IQueryable<TSource> TakeWhile<TSource> (this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,bool>> predicate);
static member TakeWhile : System.Linq.IQueryable<'Source> * System.Linq.Expressions.Expression<Func<'Source, bool>> -> System.Linq.IQueryable<'Source>
<Extension()>
Public Function TakeWhile(Of TSource) (source As IQueryable(Of TSource), predicate As Expression(Of Func(Of TSource, Boolean))) As IQueryable(Of TSource)

类型参数

TSource

source 的元素类型。

参数

source
IQueryable<TSource>

要从其返回元素的序列。

predicate
Expression<Func<TSource,Boolean>>

用于测试每个元素是否满足条件的函数。

返回

IQueryable<TSource>

一个 IQueryable<T>,包含不再通过由 predicate 指定测试的元素之前出现的输入序列中的元素。

例外

sourcepredicatenull

示例

下面的代码示例演示如何使用 TakeWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) 它从序列开头返回元素,前提是条件为 true。

string[] fruits = { "apple", "banana", "mango", "orange",
                      "passionfruit", "grape" };

// Take strings from the array until a string
// that is equal to "orange" is found.
IEnumerable<string> query =
    fruits.AsQueryable()
    .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
*/
Dim fruits() As String = {"apple", "banana", "mango", "orange", _
                      "passionfruit", "grape"}

' Take strings from the array until a string
' that is equal to "orange" is found.
Dim query = fruits.AsQueryable() _
    .TakeWhile(Function(fruit) String.Compare("orange", fruit, True) <> 0)

Dim output As New System.Text.StringBuilder
For Each fruit As String In query
    output.AppendLine(fruit)
Next

' Display the output.
MsgBox(output.ToString())

'This code produces the following output:

'apple
'banana
'mango

注解

此方法至少有一个类型参数,其类型 Expression<TDelegate> 参数为类型之 Func<T,TResult> 一。 For these parameters, you can pass in a lambda expression and it will be compiled to an Expression<TDelegate>.

该方法TakeWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>)生成一个表示调用TakeWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>)本身为构造泛型方法的一个MethodCallExpression方法。 然后,MethodCallExpressionCreateQuery(Expression)它将传递给由参数属性source表示Provider的方法IQueryProvider

执行表示调用 TakeWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) 的表达式树导致的查询行为取决于参数类型的 source 实现。 预期的行为是,它应用于predicate每个元素,source直到它找到返回的predicate``false元素。 它将返回所有元素,直到该点为止。

适用于

TakeWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32,Boolean>>)

只要指定的条件为 true,就会返回序列的元素。 将在谓词函数的逻辑中使用元素的索引。

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static System::Linq::IQueryable<TSource> ^ TakeWhile(System::Linq::IQueryable<TSource> ^ source, System::Linq::Expressions::Expression<Func<TSource, int, bool> ^> ^ predicate);
public static System.Linq.IQueryable<TSource> TakeWhile<TSource> (this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,int,bool>> predicate);
static member TakeWhile : System.Linq.IQueryable<'Source> * System.Linq.Expressions.Expression<Func<'Source, int, bool>> -> System.Linq.IQueryable<'Source>
<Extension()>
Public Function TakeWhile(Of TSource) (source As IQueryable(Of TSource), predicate As Expression(Of Func(Of TSource, Integer, Boolean))) As IQueryable(Of TSource)

类型参数

TSource

source 的元素类型。

参数

source
IQueryable<TSource>

要从其返回元素的序列。

predicate
Expression<Func<TSource,Int32,Boolean>>

用于测试每个元素是否满足条件的函数;此函数的第二个参数表示源序列中元素的索引。

返回

IQueryable<TSource>

一个 IQueryable<T>,包含不再通过由 predicate 指定测试的元素之前出现的输入序列中的元素。

例外

sourcepredicatenull

示例

下面的代码示例演示如何使用 TakeWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32,Boolean>>) 从序列开头返回元素,前提是使用元素索引的条件为 true。

string[] fruits = { "apple", "passionfruit", "banana", "mango",
                      "orange", "blueberry", "grape", "strawberry" };

// Take strings from the array until a string whose length
// is less than its index in the array is found.
IEnumerable<string> query =
    fruits.AsQueryable()
    .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
*/
Dim fruits() As String = _
    {"apple", "passionfruit", "banana", "mango", _
     "orange", "blueberry", "grape", "strawberry"}

' Take strings from the array until a string whose length
' is less than its index in the array is found.
Dim query = fruits.AsQueryable() _
    .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
MsgBox(output.ToString())

' This code produces the following output:

' apple
' passionfruit
' banana
' mango
' orange
' blueberry

注解

此方法至少有一个类型参数,其类型 Expression<TDelegate> 参数为类型之 Func<T,TResult> 一。 For these parameters, you can pass in a lambda expression and it will be compiled to an Expression<TDelegate>.

该方法TakeWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32,Boolean>>)生成一个表示调用TakeWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32,Boolean>>)本身为构造泛型方法的一个MethodCallExpression方法。 然后,MethodCallExpressionCreateQuery(Expression)它将传递给由参数属性source表示Provider的方法IQueryProvider

执行表示调用 TakeWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32,Boolean>>) 的表达式树导致的查询行为取决于参数类型的 source 实现。 预期的行为是,它应用于predicate每个元素,source直到它找到返回的predicate``false元素。 它将返回所有元素,直到该点为止。 每个源元素的索引作为第 predicate二个参数提供。

适用于