Queryable.SkipWhile 方法

定义

如果指定的条件为 true,则跳过序列中的元素,然后返回剩余的元素。Bypasses elements in a sequence as long as a specified condition is true and then returns the remaining elements.

重载

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

如果指定的条件为 true,则跳过序列中的元素,然后返回剩余的元素。Bypasses elements in a sequence as long as a specified condition is true and then returns the remaining elements.

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

如果指定的条件为 true,则跳过序列中的元素,然后返回剩余的元素。Bypasses elements in a sequence as long as a specified condition is true and then returns the remaining elements. 将在谓词函数的逻辑中使用元素的索引。The element's index is used in the logic of the predicate function.

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

如果指定的条件为 true,则跳过序列中的元素,然后返回剩余的元素。Bypasses elements in a sequence as long as a specified condition is true and then returns the remaining elements.

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

类型参数

TSource

source 的元素类型。The type of the elements of source.

参数

source
IQueryable<TSource>

要从中返回元素的 IQueryable<T>An IQueryable<T> to return elements from.

predicate
Expression<Func<TSource,Boolean>>

用于测试每个元素是否满足条件的函数。A function to test each element for a condition.

返回

IQueryable<TSource>

一个 IQueryable<T>,包含从未通过 source 指定测试的线性系列中的第一个元素开始的 predicate 中的元素。An IQueryable<T> that contains elements from source starting at the first element in the linear series that does not pass the test specified by predicate.

异常

sourcepredicatenullsource or predicate is null.

示例

下面的代码示例演示了在条件为 true 的情况下,如何使用 SkipWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) 跳过数组的元素。The following code example demonstrates how to use SkipWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) to skip elements of an array as long as a condition is true.

int[] grades = { 59, 82, 70, 56, 92, 98, 85 };

// Get all grades less than 80 by first
// sorting the grades in descending order and then
// taking all the grades after the first grade
// that is less than 80.
IEnumerable<int> lowerGrades =
    grades.AsQueryable()
    .OrderByDescending(grade => grade)
    .SkipWhile(grade => grade >= 80);

Console.WriteLine("All grades below 80:");
foreach (int grade in lowerGrades)
    Console.WriteLine(grade);

/*
    This code produces the following output:

    All grades below 80:
    70
    59
    56
*/

Dim grades() As Integer = {59, 82, 70, 56, 92, 98, 85}

' Get all grades less than 80 by first  sorting the grades 
' in descending order and then taking all the grades that 
' occur after the first grade that is less than 80.
Dim lowerGrades = grades.AsQueryable() _
    .OrderByDescending(Function(grade) grade) _
    .SkipWhile(Function(grade) grade >= 80)

Dim output As New System.Text.StringBuilder
output.AppendLine("All grades below 80:")
For Each grade As Integer In lowerGrades
    output.AppendLine(grade)
Next

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

' This code produces the following output:

' All grades below 80:
' 70
' 59
' 56

注解

此方法至少具有一个类型 Expression<TDelegate> 的参数,其类型参数为 Func<T,TResult> 类型之一。This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the Func<T,TResult> types. 对于这些参数,可以传入 lambda 表达式,并将其编译为 Expression<TDelegate>For these parameters, you can pass in a lambda expression and it will be compiled to an Expression<TDelegate>.

SkipWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) 方法生成一个 MethodCallExpression,它表示作为构造的泛型方法调用 SkipWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) 本身。The SkipWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) method generates a MethodCallExpression that represents calling SkipWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) itself as a constructed generic method. 然后,它将 MethodCallExpression 传递到 IQueryProviderCreateQuery(Expression) 方法(由 source 参数的 Provider 属性表示)。It then passes the MethodCallExpression to the CreateQuery(Expression) method of the IQueryProvider represented by the Provider property of the source parameter.

因执行表示调用 SkipWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) 的表达式树而发生的查询行为取决于 source 参数类型的实现。The query behavior that occurs as a result of executing an expression tree that represents calling SkipWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) depends on the implementation of the type of the source parameter. 预期的行为是将 predicate 应用到 source 中的每个元素,直到找到 predicate 为其返回 false 的元素。The expected behavior is that it applies predicate to each element in source until it finds an element for which predicate returns false. 返回该元素和所有剩余元素。That element and all the remaining elements are returned.

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

如果指定的条件为 true,则跳过序列中的元素,然后返回剩余的元素。Bypasses elements in a sequence as long as a specified condition is true and then returns the remaining elements. 将在谓词函数的逻辑中使用元素的索引。The element's index is used in the logic of the predicate function.

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

类型参数

TSource

source 的元素类型。The type of the elements of source.

参数

source
IQueryable<TSource>

要从中返回元素的 IQueryable<T>An IQueryable<T> to return elements from.

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

用于测试每个元素是否满足条件的函数;此函数的第二个参数表示源元素的索引。A function to test each element for a condition; the second parameter of this function represents the index of the source element.

返回

IQueryable<TSource>

一个 IQueryable<T>,包含从未通过 source 指定测试的线性系列中的第一个元素开始的 predicate 中的元素。An IQueryable<T> that contains elements from source starting at the first element in the linear series that does not pass the test specified by predicate.

异常

sourcepredicatenullsource or predicate is null.

示例

下面的代码示例演示如何使用 SkipWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32,Boolean>>) 跳过数组中的元素,前提是该元素的索引为 true 的条件。The following code example demonstrates how to use SkipWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32,Boolean>>) to skip elements of an array as long as a condition that depends on the element's index is true.

int[] amounts = { 5000, 2500, 9000, 8000, 
                    6500, 4000, 1500, 5500 };

// Skip over amounts in the array until the first amount
// that is less than or equal to the product of its
// index in the array and 1000. Take the remaining items.
IEnumerable<int> query =
    amounts.AsQueryable()
    .SkipWhile((amount, index) => amount > index * 1000);

foreach (int amount in query)
    Console.WriteLine(amount);

/*
    This code produces the following output:

    4000
    1500
    5500
*/

Dim amounts() As Integer = {5000, 2500, 9000, 8000, _
                    6500, 4000, 1500, 5500}

' Skip over amounts in the array until the first amount
' that is less than or equal to the product of its
' index in the array and 1000. Take the remaining items.
Dim query = amounts.AsQueryable() _
    .SkipWhile(Function(amount, index) amount > index * 1000)

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

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

' This code produces the following output:

' 4000
' 1500
' 5500

注解

此方法至少具有一个类型 Expression<TDelegate> 的参数,其类型参数为 Func<T,TResult> 类型之一。This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the Func<T,TResult> types. 对于这些参数,可以传入 lambda 表达式,并将其编译为 Expression<TDelegate>For these parameters, you can pass in a lambda expression and it will be compiled to an Expression<TDelegate>.

SkipWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32,Boolean>>) 方法生成一个 MethodCallExpression,它表示作为构造的泛型方法调用 SkipWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32,Boolean>>) 本身。The SkipWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32,Boolean>>) method generates a MethodCallExpression that represents calling SkipWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32,Boolean>>) itself as a constructed generic method. 然后,它将 MethodCallExpression 传递到 IQueryProviderCreateQuery(Expression) 方法(由 source 参数的 Provider 属性表示)。It then passes the MethodCallExpression to the CreateQuery(Expression) method of the IQueryProvider represented by the Provider property of the source parameter.

因执行表示调用 SkipWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32,Boolean>>) 的表达式树而发生的查询行为取决于 source 参数类型的实现。The query behavior that occurs as a result of executing an expression tree that represents calling SkipWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32,Boolean>>) depends on the implementation of the type of the source parameter. 预期的行为是将 predicate 应用到 source 中的每个元素,直到找到 predicate 为其返回 false 的元素。The expected behavior is that it applies predicate to each element in source until it finds an element for which predicate returns false. 返回该元素和所有剩余元素。That element and all the remaining elements are returned. 每个源元素的索引都作为 predicate的第二个参数提供。The index of each source element is provided as the second argument to predicate.

适用于