LinqToObject和LinqToSql的区别

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

 

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们