LinqToObject和LinqToSql的區別

陳大寶發表於2021-05-05

抓住五一假期尾巴和小夥伴們一起分享這兩者的區別、大家在日常編碼的過程當中肯定也注意過或者使用過、但是二者其實存在本質的區別

1、什麼是LinqToObject呢?

LINQ to Objects指直接將 LINQ 查詢與任何 IEnumerable 或 IEnumerable<T> 集合一起使用,而不使用中間 LINQ 提供程式或 API,例如 LINQ to SQL 或 LINQ to XML。 簡單來說它是一種操作的方式、方法,從根本上說,“LINQ to Objects”表示一種新的處理集合的方法。 採用舊方法,必須編寫指定如何從集合檢索資料的複雜的 foreach 迴圈。 而採用 LINQ 方法,只需編寫描述要檢索的內容的宣告性程式碼。

2、什麼是LinqToSQL呢?

LINQ to SQL 是 .NET Framework 版本3.5 的一個元件,它提供用於將關係資料作為物件管理的執行時基礎結構。在 LINQ to SQL 中,關聯式資料庫的資料模型對映到用開發人員所用的程式語言表示的物件模型。 當應用程式執行時,LINQ to SQL 會將物件模型中的語言整合查詢轉換為 SQL,然後將它們傳送到資料庫進行執行。 當資料庫返回結果時,LINQ to SQL 會將它們轉換回您可以用您自己的程式語言處理的物件。

3、二者區別

LinqToObject:返回的是IEnumerable型別,資料其實已經在記憶體裡,有個迭代器的實現,引數用的是委託

LinqToSql:返回的IQueryable型別,資料在資料庫裡面,這個list裡面有表示式目錄樹---返回值型別--IQueryProvider(查詢的支援工具,sqlserver語句的生成),其實userList只是一個包裝物件,裡面有表示式目錄樹,有結果型別,有解析工具,還有上下文,真需要資料的時候才去解析sql,執行sql,拿到資料的

一、通常LinqToSql 和我們的ORM框架結合使用、其內部是一個表示式目錄樹(也叫二叉樹)、也就是LinqToSql 通過表示式式目錄樹對其進行拼接後、拼接完成後一次性轉換成SQL語句至資料庫中查詢、基於資料庫查詢

二、我們的LinqToObject是將我們資料一次性從資料庫中查詢出來並放置記憶體中、然後通過記憶體中的資料進行過濾、篩選出我們的目標資料、基於記憶體查詢

以下不難看出IQueryable繼承自IEnumerable 但是二者卻有著本質的區別

    //
    // 摘要:
    //     提供針對特定資料來源(其中資料型別未未知)評估查詢的功能。
    //
    // 型別引數:
    //   T:
    //     資料來源中資料的型別。
    public interface IQueryable<out T> : IEnumerable<T>, IEnumerable, IQueryable
    {
    }

這是IQueryable一些實現 包含表示式目錄樹Expression引數並內建委託

  //
        // 摘要:
        //     Filters a sequence of values based on a predicate. Each element's index is used
        //     in the logic of the predicate function.
        //
        // 引數:
        //   source:
        //     An System.Linq.IQueryable`1 to filter.
        //
        //   predicate:
        //     A function to test each element for a condition; the second parameter of the
        //     function represents the index of the element in the source sequence.
        //
        // 型別引數:
        //   TSource:
        //     The type of the elements of source.
        //
        // 返回結果:
        //     An System.Linq.IQueryable`1 that contains elements from the input sequence that
        //     satisfy the condition specified by predicate.
        //
        // 異常:
        //   T:System.ArgumentNullException:
        //     source or predicate is null.
        public static IQueryable<TSource> Where<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, int, bool>> predicate);

這是IEnumerable型別一些實現 並不是表示式目錄樹僅僅是一個委託來實現

 1         //
 2         // 摘要:
 3         //     Filters a sequence of values based on a predicate. Each element's index is used
 4         //     in the logic of the predicate function.
 5         //
 6         // 引數:
 7         //   source:
 8         //     An System.Collections.Generic.IEnumerable`1 to filter.
 9         //
10         //   predicate:
11         //     A function to test each source element for a condition; the second parameter
12         //     of the function represents the index of the source element.
13         //
14         // 型別引數:
15         //   TSource:
16         //     The type of the elements of source.
17         //
18         // 返回結果:
19         //     An System.Collections.Generic.IEnumerable`1 that contains elements from the input
20         //     sequence that satisfy the condition.
21         //
22         // 異常:
23         //   T:System.ArgumentNullException:
24         //     source or predicate is null.
25         public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, Func<TSource, int, bool> predicate);