LinqToObject和LinqToSql的區別

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

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);

 

Tags: