Queryable.Where 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
根据谓词筛选值序列。
重载
| 名称 | 说明 |
|---|---|
| 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>>)
- Source:
- Queryable.cs
- Source:
- Queryable.cs
- Source:
- Queryable.cs
- Source:
- Queryable.cs
- Source:
- Queryable.cs
根据谓词筛选值序列。
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);
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")]
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>
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")>]
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>>
用于测试条件的每个元素的函数。
返回
一个包含输入序列中满足由predicate指定条件的元素的元素IQueryable<T>。
- 属性
例外
source 或 predicate 为 null.
示例
下面的代码示例演示如何用于 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> 一。 对于这些参数,可以传入 lambda 表达式,并将它编译为一个 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>>)
- Source:
- Queryable.cs
- Source:
- Queryable.cs
- Source:
- Queryable.cs
- Source:
- Queryable.cs
- Source:
- Queryable.cs
根据谓词筛选值序列。 每个元素的索引都在谓词函数的逻辑中使用。
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);
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")]
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>
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")>]
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>>
用于测试条件的每个元素的函数;函数的第二个参数表示源序列中元素的索引。
返回
一个包含输入序列中满足由predicate指定条件的元素的元素IQueryable<T>。
- 属性
例外
source 或 predicate 为 null.
示例
下面的代码示例演示如何 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> 一。 对于这些参数,可以传入 lambda 表达式,并将它编译为一个 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 实现。 预期行为是,它返回满足指定predicate条件的元素source。 每个源元素的索引作为第二个参数 predicate提供。