如果IQueryable <T>继承IEnumerable <T>,为什么IQueryable <T>是LINQ to SQL?

JAN*_*JAN 4 c# linq ienumerable iqueryable visual-studio-2015

IEnumerable<T>case是LINQ-to-object,IQueryable<T>是允许LINQ-to-SQL的接口,但是IQueryable<T>继承IEnumerable<T>,所以如何IQueryable<T>IEnumerable<T>过滤内存中的对象时过滤数据库中的行?

InB*_*een 8

您错误地认为继承和接口实现.

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 }
}
Run Code Online (Sandbox Code Playgroud)

Bar实现IFoo通过的事实IBar并不意味着,无论如何,Foo和之间存在任何关系Bar.