簡體   English   中英

如果IQueryable <T> 繼承IEnumerable <T> 怎么來IQueryable <T> 是LINQ to SQL?

[英]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的事實並不意味着FooBar之間存在任何關系。

當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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM