Queryable.SkipWhile Method

Definition

Bypasses elements in a sequence as long as a specified condition is true and then returns the remaining elements.

Overloads

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

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

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

Source:
Queryable.cs
Source:
Queryable.cs
Source:
Queryable.cs

Bypasses elements in a sequence as long as a specified condition is true and then returns the remaining elements.

C#
public static System.Linq.IQueryable<TSource> SkipWhile<TSource>(this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,bool>> predicate);

Type Parameters

TSource

The type of the elements of source.

Parameters

source
IQueryable<TSource>

An IQueryable<T> to return elements from.

predicate
Expression<Func<TSource,Boolean>>

A function to test each element for a condition.

Returns

IQueryable<TSource>

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.

Exceptions

source or predicate is null.

Examples

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.

C#
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
*/

Remarks

This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an Expression<TDelegate>.

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. It then passes the MethodCallExpression to the CreateQuery(Expression) method of the IQueryProvider represented by the Provider property of the source parameter.

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

Applies to

.NET 9 and other versions
Product Versions
.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 2.0, 2.1
UWP 10.0

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

Source:
Queryable.cs
Source:
Queryable.cs
Source:
Queryable.cs

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.

C#
public static System.Linq.IQueryable<TSource> SkipWhile<TSource>(this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,int,bool>> predicate);

Type Parameters

TSource

The type of the elements of source.

Parameters

source
IQueryable<TSource>

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.

Returns

IQueryable<TSource>

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.

Exceptions

source or predicate is null.

Examples

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.

C#
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
*/

Remarks

This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an Expression<TDelegate>.

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. It then passes the MethodCallExpression to the CreateQuery(Expression) method of the IQueryProvider represented by the Provider property of the source parameter.

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. 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. The index of each source element is provided as the second argument to predicate.

Applies to

.NET 9 and other versions
Product Versions
.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 2.0, 2.1
UWP 10.0