Enumerable.Where 方法

定义

基于谓词筛选值序列。

重载

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

基于谓词筛选值序列。

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

基于谓词筛选值序列。 将在谓词函数的逻辑中使用每个元素的索引。

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

Source:
Where.cs
Source:
Where.cs
Source:
Where.cs

基于谓词筛选值序列。

C#
public static System.Collections.Generic.IEnumerable<TSource> Where<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>

例外

sourcepredicatenull

示例

下面的代码示例演示如何使用 Where<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) 筛选序列。

C#
List<string> fruits =
    new List<string> { "apple", "passionfruit", "banana", "mango",
                    "orange", "blueberry", "grape", "strawberry" };

IEnumerable<string> query = fruits.Where(fruit => fruit.Length < 6);

foreach (string fruit in query)
{
    Console.WriteLine(fruit);
}
/*
 This code produces the following output:

 apple
 mango
 grape
*/

注解

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

在查询表达式语法中, where (C#) 或 Where (Visual Basic) 子句将转换为 的调用 Where<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)

另请参阅

适用于

.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

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

Source:
Where.cs
Source:
Where.cs
Source:
Where.cs

基于谓词筛选值序列。 将在谓词函数的逻辑中使用每个元素的索引。

C#
public static System.Collections.Generic.IEnumerable<TSource> Where<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>

例外

sourcepredicatenull

示例

下面的代码示例演示如何使用 Where<TSource>(IEnumerable<TSource>, Func<TSource,Int32,Boolean>) 根据涉及每个元素的索引的谓词筛选序列。

C#
int[] numbers = { 0, 30, 20, 15, 90, 85, 40, 75 };

IEnumerable<int> query =
    numbers.Where((number, index) => number <= index * 10);

foreach (int number in query)
{
    Console.WriteLine(number);
}
/*
 This code produces the following output:

 0
 20
 15
 40
*/

注解

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

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

适用于

.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