Queryable.Where 方法

定义

基于谓词筛选值序列。

重载

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

基于谓词筛选值序列。

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

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

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

基于谓词筛选值序列。

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

类型参数

TSource

source 的元素类型。

参数

source
IQueryable<TSource>

要筛选的 IQueryable<T>

predicate
Expression<Func<TSource,Boolean>>

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

返回

IQueryable<TSource>

一个 IQueryable<T>,包含满足由 predicate 指定的条件的输入序列中的元素。

例外

sourcepredicatenull

示例

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

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

// Get all strings whose length is less than 6.
IEnumerable<string> query =
    fruits.AsQueryable().Where(fruit => fruit.Length < 6);

foreach (string fruit in query)
    Console.WriteLine(fruit);

/*
    This code produces the following output:

    apple
    mango
    grape
*/
Dim fruits As New List(Of String)(New String() _
                        {"apple", "passionfruit", "banana", "mango", _
                         "orange", "blueberry", "grape", "strawberry"})

' Get all strings whose length is less than 6.
Dim query = fruits.AsQueryable().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
MsgBox(output.ToString())

' This code produces the following output:

' apple
' mango
' grape

注解

此方法至少有一个类型参数,其类型 Expression<TDelegate> 参数为类型之 Func<T,TResult> 一。 For these parameters, you can pass in a lambda expression and it will be compiled to an Expression<TDelegate>.

该方法Where<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>)生成一个表示调用Where<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>)本身为构造泛型方法的一个MethodCallExpression方法。 然后,MethodCallExpressionCreateQuery(Expression)它将传递给由参数属性source表示Provider的方法IQueryProvider

执行表示调用 Where<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) 的表达式树导致的查询行为取决于参数类型的 source 实现。 预期行为是,它返回满足由该predicate条件指定的条件的元素source

适用于

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

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

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

类型参数

TSource

source 的元素类型。

参数

source
IQueryable<TSource>

要筛选的 IQueryable<T>

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

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

返回

IQueryable<TSource>

一个 IQueryable<T>,包含满足由 predicate 指定的条件的输入序列中的元素。

例外

sourcepredicatenull

示例

下面的代码示例演示如何根据 Where<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32,Boolean>>) 合并每个元素索引的谓词筛选序列。

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

// Get all the numbers that are less than or equal to
// the product of their index in the array and 10.
IEnumerable<int> query =
    numbers.AsQueryable()
    .Where((number, index) => number <= index * 10);

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

/*
    This code produces the following output:

    0
    20
    15
    40
*/
Dim numbers() As Integer = {0, 30, 20, 15, 90, 85, 40, 75}

' Get all the numbers that are less than or equal to
' the product of their index in the array and 10.
Dim query = numbers.AsQueryable() _
    .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
MsgBox(output.ToString())

' This code produces the following output:

' 0
' 20
' 15
' 40

注解

此方法至少有一个类型参数,其类型 Expression<TDelegate> 参数为类型之 Func<T,TResult> 一。 For these parameters, you can pass in a lambda expression and it will be compiled to an Expression<TDelegate>.

该方法Where<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32,Boolean>>)生成一个表示调用Where<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32,Boolean>>)本身为构造泛型方法的一个MethodCallExpression方法。 然后,MethodCallExpressionCreateQuery(Expression)它将传递给由参数属性source表示Provider的方法IQueryProvider

执行表示调用 Where<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32,Boolean>>) 的表达式树导致的查询行为取决于参数类型的 source 实现。 预期行为是,它返回满足 sourcepredicate指定条件的元素。 每个源元素的索引作为第二个参数 predicate提供。

适用于