Enumerable.SkipWhile 方法

定义

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

重载

SkipWhile<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)

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

SkipWhile<TSource>(IEnumerable<TSource>, Func<TSource,Int32,Boolean>)

如果指定的条件为 true,则跳过序列中的元素,然后返回剩余的元素。 将在谓词函数的逻辑中使用元素的索引。

SkipWhile<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)

Source:
Skip.cs
Source:
Skip.cs
Source:
Skip.cs

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

C#
public static System.Collections.Generic.IEnumerable<TSource> SkipWhile<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,bool> predicate);

类型参数

TSource

source 的元素类型。

参数

source
IEnumerable<TSource>

要从中返回元素的 IEnumerable<T>

predicate
Func<TSource,Boolean>

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

返回

IEnumerable<TSource>

一个 IEnumerable<T>,包含输入序列中的元素,该输入序列从线性系列中没有通过 predicate 指定测试的第一个元素开始。

例外

sourcepredicatenull

示例

下面的代码示例演示如何使用 SkipWhile<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) 跳过数组的元素(只要条件为 true)。

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

IEnumerable<int> lowerGrades =
    grades
    .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
*/

注解

方法是 SkipWhile<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) 使用延迟执行实现的。 即时返回值是一个对象,用于存储执行操作所需的所有信息。 在通过直接调用GetEnumerator其方法或在 C# For Eachforeach Visual Basic 中使用 来枚举对象之前,不会执行此方法表示的查询。

此方法使用 predicate 测试 的每个source元素,如果结果为 true,则跳过 元素。 谓词函数为 元素返回 false 后,将生成该元素和 中的 source 剩余元素,并且不再 predicate调用 。

如果 predicate 为序列中的所有元素返回 true ,则返回一个空 IEnumerable<T>

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

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

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.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 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

SkipWhile<TSource>(IEnumerable<TSource>, Func<TSource,Int32,Boolean>)

Source:
Skip.cs
Source:
Skip.cs
Source:
Skip.cs

如果指定的条件为 true,则跳过序列中的元素,然后返回剩余的元素。 将在谓词函数的逻辑中使用元素的索引。

C#
public static System.Collections.Generic.IEnumerable<TSource> SkipWhile<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,int,bool> predicate);

类型参数

TSource

source 的元素类型。

参数

source
IEnumerable<TSource>

要从中返回元素的 IEnumerable<T>

predicate
Func<TSource,Int32,Boolean>

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

返回

IEnumerable<TSource>

一个 IEnumerable<T>,包含输入序列中的元素,该输入序列从线性系列中没有通过 predicate 指定测试的第一个元素开始。

例外

sourcepredicatenull

示例

下面的代码示例演示如何使用 SkipWhile<TSource>(IEnumerable<TSource>, Func<TSource,Int32,Boolean>) 跳过数组的元素,前提是依赖于元素索引的条件为 true。

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

IEnumerable<int> query =
    amounts.SkipWhile((amount, index) => amount > index * 1000);

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

/*
 This code produces the following output:

 4000
 1500
 5500
*/

注解

此方法通过使用延迟执行来实现。 即时返回值是一个对象,用于存储执行操作所需的所有信息。 在通过直接调用GetEnumerator其方法或在 C# For Eachforeach Visual Basic 中使用 来枚举对象之前,不会执行此方法表示的查询。

方法SkipWhile<TSource>(IEnumerable<TSource>, Func<TSource,Int32,Boolean>)使用 predicate 测试 的每个元素source,如果结果为 true,则跳过 元素。 谓词函数为 元素返回 false 后,将生成该元素和 中的 source 剩余元素,并且不再 predicate调用 。

如果 predicate 为序列中的所有元素返回 true ,则返回一个空 IEnumerable<T>

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

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

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

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.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 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0