
[英]Dynamic LINQ OrderBy on IEnumerable<T> / IQueryable<T>
[英]If IQueryable<T> inherits IEnumerable<T> , how come IQueryable<T> is LINQ to SQL?
IEnumerable<T>
案例是LINQ到对象, IQueryable<T>
是允许LINQ-to-SQL的接口,但IQueryable<T>
继承IEnumerable<T>
,那么IQueryable<T>
如何过滤呢?数据库中的行,而IEnumerable<T>
过滤内存中的对象?
您错误地认为继承和接口实现。
IQueryable<T>
继承IEnumerable<T>
但这与派生类继承基类实现的类之间的继承完全不同 。 接口只是契约,它们背后没有实际代码:
public interface IFoo
{
void Foo(); //its simply a contract, no implementation at all.
}
public interface IBar: IFoo
{
void Bar();
}
public class Foo: IFoo
{
void Foo() { //Foo's sepecific implementation goes here }
}
public class Bar : IBar
{
void Foo() { //Bar's specific implementation goes here }
void Bar() { //implementation goes here }
}
Bar
通过IBar
实现IFoo
的事实并不意味着Foo
和Bar
之间存在任何关系。
当IEnumerable过滤内存中的对象时,IQueryable如何过滤数据库中的行?
他们没有,这是一个不准确的陈述。
实际的实现代码是扩展方法,它们对IEnumerable或IQueryable有选择性。
在EF有一个
Where<T>(this IQueryable<T>, Expression<Predicate<T>> )
在System.Linq中有
Where<T>(this IEnumerable<T>, Predicate<T>)
resoluton规则将优先于基本Linq one的IQueryable扩展,并且通过Expression<>
参数启用魔术。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.