当前位置:网站首页>IQueryable和IEnumerable的区别
IQueryable和IEnumerable的区别
2022-06-22 05:03:00 【我把生活交给了工作】
IEnumerable接口
公开枚举器,该枚举器支持在指定类型的集合上进行简单迭代。也就是说:实现了此接口的object,就可以直接使用foreach遍历此object;
IQueryable 接口
它继承 IEnumerable 接口,而因为.net版本加入Linq和IQueryable后,使得IEnumerable不再那么单调,变得更加强大和丰富。
为了区别两个接口,我们通过一个实际的例子来解释一下。
[TestMethod]
public void LinqTest()
{
var db = new SecurityRepositoryContext(new HSLogger());
var ur = new UserRepository(db);
//查询的结果放入IQueryable接口的集合中
IQueryable<User> querys = (from c in db.Context.Set<User>()
orderby c.Code
select c).Skip<User>(2).Take<User>(2);
//注意这个AsEnumerable<User>()在分页查询之前,先将其转换成IEnumerable类型
IEnumerable<User> enumers = (from c in db.Context.Set<User>()
orderby c.Code
select c).AsEnumerable<User>().Skip<User>(2).Take<User>(2);
//因为启用了延迟加载机制,所以下面调用一下,才会真正去读取数据库
foreach (var c in querys)
{
//TODO
}
foreach (var c in enumers)
{
//TODO
}
}
跟踪一下代码,看一下这两段在执行时所生成的SQL代码:
第一段,返回IQueryable接口类型:
SELECT
[Extent1].[Id] AS [Id],
[Extent1].[Code] AS [Code],
[Extent1].[Name] AS [Name],
[Extent1].[Status] AS [Status],
[Extent1].[IsOnline] AS [IsOnline],
[Extent1].[SystemVersion] AS [SystemVersion],
[Extent1].[CreatedBy] AS [CreatedBy],
[Extent1].[CreatedOn] AS [CreatedOn],
[Extent1].[ModifiedBy] AS [ModifiedBy],
[Extent1].[ModifiedOn] AS [ModifiedOn],
[Extent1].[VersionNumber] AS [VersionNumber]
FROM [dbo].[User] AS [Extent1]
ORDER BY [Extent1].[Code] ASC
OFFSET 2 ROWS FETCH NEXT 2 ROWS ONLY
第二段,返回IEnumerable接口类型:
SELECT
[Extent1].[Id] AS [Id],
[Extent1].[Code] AS [Code],
[Extent1].[Name] AS [Name],
[Extent1].[Status] AS [Status],
[Extent1].[IsOnline] AS [IsOnline],
[Extent1].[SystemVersion] AS [SystemVersion],
[Extent1].[CreatedBy] AS [CreatedBy],
[Extent1].[CreatedOn] AS [CreatedOn],
[Extent1].[ModifiedBy] AS [ModifiedBy],
[Extent1].[ModifiedOn] AS [ModifiedOn],
[Extent1].[VersionNumber] AS [VersionNumber]
FROM [dbo].[User] AS [Extent1]
ORDER BY [Extent1].[Code] ASC
通过上面两个例子我总结了一下,IQueryable接口与IEnumberable接口的区别:
IEnumerable 泛型类在调用自己的SKip 和 Take 等扩展方法之前数据就已经加载在本地内存里了,而IQueryable 是将Skip ,take 这些方法表达式翻译成T-SQL语句之后再向SQL服务器发送命令,它并不是把所有数据都加载到内存里来才进行条件过滤。
边栏推荐
- Compilation des connaissances communes de la base de données numpy
- 学信网的头像下载下来太小的处理方法
- Monorepo Sliding methodology: Reference module Hot Update
- Virtual address space
- 《数据库原理》期末考试题
- 重构思维系列2-函数及变量
- 【故障诊断】切图代码切不出来,slide.read_region不运行,但程序不报错
- Ora-15063: ASM discovered an insufficient number of disks for diskgroup
- Cloud native enthusiast weekly: Chaos mesh upgraded to CNCF incubation project
- go学习(二、内建容器)
猜你喜欢

VirtualBox 6.1.34 release

Slurm tutorial

Will swift compile to native code- Does Swift compile to native code?
![[camp] at the beginning, improve [leopard] power - vivo activity plug-in management platform](/img/75/6a129de59c21b783622ba31f77f647.jpg)
[camp] at the beginning, improve [leopard] power - vivo activity plug-in management platform

Detailed explanation of deep learning technology for building an image search engine that can find similar images

Pull down refresh, push up load (easy to use, finally)

UC San Diego | evit: using token recombination to accelerate visual transformer (iclr2022)

厉害了!淮北两企业获准使用地理标志产品专用标志

Postman uses (reads) JSON files for batch testing

数据库---基础知识
随机推荐
mysql day01课堂笔记
数据的备份与恢复
mysql常用sql
Virtual address space
Compilation des connaissances communes de la base de données numpy
Web design and production final assignment report -- animator Hayao Miyazaki
[camp] at the beginning, improve [leopard] power - vivo activity plug-in management platform
MySQL notes
Recruiters - issue 23
numpy库常用知识整理
9 practical shell scripts, recommended collection!
Remote Dictionary Server(Redis)——基于 KV 结构的作为 Cache 使用的 NoSQL 数据库管理系统
Postman uses (reads) JSON files for batch testing
How to deal with too small picture downloaded from xuexin.com
基于深度学习的目标检测算法面试必备(RCNN~YOLOv5)
What is the difference between TOC and Tob
花式优化器整理
并发编程——线程池
Zhongmin online: sharing online channel resources with "points" as the link
A domestic developer opened the "programmer's Guide to cooking" and became popular!