当前位置:网站首页>The difference between iqueryable and IEnumerable
The difference between iqueryable and IEnumerable
2022-06-22 05:08:00 【I left my life to work】
IEnumerable Interface
Expose enumerator , The enumerator supports simple iterations over a collection of a specified type . in other words : Implemented this interface object, You can use it directly foreach Traverse this object;
IQueryable Interface
It inherited IEnumerable Interface , And because .net Version to join Linq and IQueryable after , bring IEnumerable No longer so monotonous , Become stronger and richer .
To distinguish the two interfaces , Let's use a practical example to explain .
[TestMethod]
public void LinqTest()
{
var db = new SecurityRepositoryContext(new HSLogger());
var ur = new UserRepository(db);
// The results of the query are put into IQueryable In the collection of interfaces
IQueryable<User> querys = (from c in db.Context.Set<User>()
orderby c.Code
select c).Skip<User>(2).Take<User>(2);
// Pay attention to this AsEnumerable<User>() Before paging queries , First convert it into IEnumerable type
IEnumerable<User> enumers = (from c in db.Context.Set<User>()
orderby c.Code
select c).AsEnumerable<User>().Skip<User>(2).Take<User>(2);
// Because the delayed loading mechanism is enabled , So let's call , To actually read the database
foreach (var c in querys)
{
//TODO
}
foreach (var c in enumers)
{
//TODO
}
}
Trace the code , Take a look at the generated by these two paragraphs when they are executed SQL Code :
The first paragraph , return IQueryable Interface type :
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
The second paragraph , return IEnumerable Interface type :
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
Through the above two examples, I have summarized ,IQueryable Interface and IEnumberable Interface differences :
IEnumerable Generic classes are calling their own SKip and Take Before the extension method, the data has been loaded into the local memory , and IQueryable Yes, it will Skip ,take These method expressions are translated into T-SQL Statement and then to SQL The server sends commands , It does not load all data into memory to perform conditional filtering .
边栏推荐
- Data backup and recovery
- Remote dictionary server (redis) - a kV based NoSQL database management system used as a cache
- numpy庫常用知識整理
- Create a new local content and upload it to the code cloud branch
- [user guide] use of Tsinghua source
- Debugging wechat built-in browser with chrome
- MySQL day03 class notes
- Solve the shortage of developers. Maybe it can help you
- NLP 的 不可能三角?
- 9. Gateway cross domain processing
猜你喜欢

Great! Huaibei and Huaibei enterprises are approved to use special marks for geographical indication products

9. Gateway cross domain processing

Software architecture and pattern: structure, component and relationship

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

IDP depth | what kind of data analysis and mining tools do enterprises need?

Disturbed when programmers are programming? Daily anecdotes

Ora-15063: ASM discovered an insufficient number of disks for diskgroup

【故障诊断】stitch.py脚本失效

JUC - thread interrupt and thread waiting and wakeup (locksupport)

This is a picture
随机推荐
厉害了!淮北两企业获准使用地理标志产品专用标志
重构思维系列2-函数及变量
Web design and production final assignment report -- animator Hayao Miyazaki
yarn部署模式依赖预上传设置
NLP 的 不可能三角?
6. Local - custom filter factory
zipimport. ZipImportError:
这是一个图片
新建本地内容上传至码云分支
Monorepo絲滑方法論:引用模塊熱更新
Getting the return value of DPI function is always 96 | getting the return value of DPI function is incorrect | why the return value of getdpiformonitor/getdevicecaps is incorrect
【故障诊断】stitch.py脚本失效
Monorepo Sliding methodology: Reference module Hot Update
Ora-15063: ASM discovered an insufficient number of disks for diskgroup
Liunx virtual machine environment uses docker to install Oracle database and Navicat to connect
并发编程——线程池
Chapter 6 infinite series_ Fourier series
【故障诊断】cv2.imwrite无法写入图片,但程序就是不报错
Flynk deployment mode summary
【故障诊断】CUDA kernel errors might be asynchronously reported at some other API call,so the stacktrace b