If IQueryable <T> inherits IEnumerable <T>, why is IQueryable <T> LINQ to SQL?

IEnumerable<T>case is LINQ-to-object, and IQueryable<T>is the interface that LINQ-to-SQL allows, however it IQueryable<T>inherits IEnumerable<T>, so how can I IQueryable<T>filter rows in a database and IEnumerable<T>filter objects in memory?

+4
source share
2 answers

You are mistaken in inheritance and implementation of the interface.

IQueryable<T>inherits IEnumerable<T>, but this is radically different from class-to-class inheritance, where a derived class inherits an implementation of the base class. Interfaces are just contracts, there is no real code behind them:

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 sepecific implementation goes here }
}

public class Bar : IBar 
{
    void Foo() { //Bar specific implementation goes here }
    void Bar() { //implementation goes here }
}

The fact that it Barimplements IFoothrough IBardoes not in any way mean that there is any connection between Fooand Bar.

+8
source

How can IQueryable filter rows in a database while IEnumerable filters objects in memory?

They do not, this is an inaccurate statement.

The actual implementation code is inside extension methods that are selective for IEnumerable or IQueryable.

In EF there is

Where<T>(this IQueryable<T>, Expression<Predicate<T>> )

and in System.Linq there is

Where<T>(this IEnumerable<T>, Predicate<T>)

resoluton IQueryable Linq, Expression<>.

+3

Source: https://habr.com/ru/post/1673590/


All Articles