Enumerable.Where 方法

定义

基于谓词筛选值序列。

重载

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

基于谓词筛选值序列。

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

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

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

基于谓词筛选值序列。

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static System::Collections::Generic::IEnumerable<TSource> ^ Where(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, bool> ^ predicate);
public static System.Collections.Generic.IEnumerable<TSource> Where<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,bool> predicate);
static member Where : seq<'Source> * Func<'Source, bool> -> seq<'Source>
<Extension()>
Public Function Where(Of TSource) (source As IEnumerable(Of TSource), predicate As Func(Of TSource, Boolean)) As IEnumerable(Of TSource)

类型参数

TSource

source 的元素类型。

参数

source
IEnumerable<TSource>

要筛选的 IEnumerable<T>

predicate
Func<TSource,Boolean>

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

返回

IEnumerable<TSource>

一个包含输入序列中满足条件的元素的 IEnumerable<T>

例外

sourcepredicatenull

示例

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

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
*/
' Create a list of strings.
Dim fruits As New List(Of String)(New String() _
                    {"apple", "passionfruit", "banana", "mango",
                     "orange", "blueberry", "grape", "strawberry"})

' Restrict the results to those strings whose
' length is less than six.
Dim query As IEnumerable(Of String) =
fruits.Where(Function(fruit) fruit.Length < 6)

' Display the results.
Dim output As New System.Text.StringBuilder
For Each fruit As String In query
    output.AppendLine(fruit)
Next
Console.WriteLine(output.ToString())

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

另请参阅

适用于

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

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

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static System::Collections::Generic::IEnumerable<TSource> ^ Where(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, int, bool> ^ predicate);
public static System.Collections.Generic.IEnumerable<TSource> Where<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,int,bool> predicate);
static member Where : seq<'Source> * Func<'Source, int, bool> -> seq<'Source>
<Extension()>
Public Function Where(Of TSource) (source As IEnumerable(Of TSource), predicate As Func(Of TSource, Integer, Boolean)) As IEnumerable(Of TSource)

类型参数

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>) 根据涉及每个元素的索引的谓词筛选序列。

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
*/
' Create an array of integers.
Dim numbers() As Integer = {0, 30, 20, 15, 90, 85, 40, 75}

' Restrict the results to those numbers whose
' values are less than or equal to their index times 10.
Dim query As IEnumerable(Of Integer) =
numbers.Where(Function(number, index) number <= index * 10)

' Display the results.
Dim output As New System.Text.StringBuilder
For Each number As Integer In query
    output.AppendLine(number)
Next
Console.WriteLine(output.ToString())

' This code produces the following output:
'
' 0
' 20
' 15
' 40

注解

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

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

适用于