Probably really simple so please excuse my ignorance...
To my knowledge, there are a couple flavours of the Where()
extension method:
Queryable.Where<TSource> Method (IQueryable<TSource>, Expression<Func<TSource, Boolean>>)
Enumerable.Where<TSource> Method (IEnumerable<TSource>, Func<TSource, Boolean>)
Both of the above extensions are housed in the System.Linq
namespace, so that I can do Where()
at all is hopefully proof enough that I've imported the correct namespace - or is there another namespace I need for IQueryable extensions?
I understand that IQueryable<T>
inherits from IEnumerable<T>
but why can't I get the IQueryable<T>
extensions?
class Test
{
IQueryable<Test> SomeMethod(Func<T, bool> criteria)
{
return new List<Test> { new Test() }.AsQueryable().Where(criteria); // compiler error converting IEnumerable<T> to IQueryable<T>
}
}
As shown above, there should be an extension method available that returns IQueryable? Why is it resolving to the IEnumerable Extensions?
The Queryable.Where Extension Method expects an Expression<Func<T, bool>>
(ie an expression tree representing a lambda), not a Func<T, bool>
(a lambda itself).
This works:
IQueryable<Test> ApplyCriteria(IQueryable<Test> queryable,
Expression<Func<Test, bool>> criteria)
{ // ↑
return queryable.Where(criteria);
}
IQueryable<T>.Where()
仅适用于Expression<Func<T, bool>>
作为谓词参数。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.