IQueryable、ICollection、IList 和 IList 之间的区别IDictionary接口
我试图理解 IQueryable、ICollection、IList 和 IList 之间的区别。 IDictionary接口 对于迭代、索引、查询等基本操作来说,速度更快。
哪些类(例如 Collection、List、Dictionary 等)适合启动这些接口以及我们何时应该使用这些类。与其他类相比,使用这些类的基本优点。
我尝试阅读其他有类似问题的帖子,但没有任何内容回答我的完整问题。 感谢您的帮助。
I am trying to understand difference between IQueryable, ICollection, IList & IDictionary interface
which is more faster for basic operations like iterating, Indexing, Querying and more.
which class like Collection, List, Dictionary etc would be good to initiating with these interfaces and when should we be using these class. Basic advantages of using these classes over others.
I tried reading other posts with similar questions but nothing answered my full questions.
Thanks for the help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
所有这些接口都继承自
IEnumerable
,您应该确保您理解它。该接口基本上允许您在
foreach
语句(在 C# 中)中使用该类。ICollection
是您列出的最基本的接口。它是一个支持 Count 的可枚举接口,仅此而已。IList
是ICollection
的一切,但它还支持添加和删除项目、通过索引检索项目等。它是“对象列表”最常用的接口,但它是模糊的我知道。IQueryable
是一个支持 LINQ 的可枚举接口。您始终可以从IList
创建IQueryable
并使用 LINQ to Objects,但您还会发现IQueryable
用于延迟执行 LINQ 中的 SQL 语句SQL 和 LINQ to Entities。IDictionary
是一种不同的动物,因为它是唯一键到值的映射。它也是可枚举的,因为您可以枚举键/值对,但除此之外它的用途与您列出的其他用途不同。这些内容的 MSDN 文档都很不错,所以我将从这里开始完善您的理解。
All of these interfaces inherit from
IEnumerable
, which you should make sure you understand. That interface basically lets you use the class in aforeach
statement (in C#).ICollection
is the most basic of the interfaces you listed. It's an enumerable interface that supports a Count and that's about it.IList
is everything thatICollection
is, but it also supports adding and removing items, retrieving items by index, etc. It's the most commonly-used interface for "lists of objects", which is vague I know.IQueryable
is an enumerable interface that supports LINQ. You can always create anIQueryable
from anIList
and use LINQ to Objects, but you also findIQueryable
used for deferred execution of SQL statements in LINQ to SQL and LINQ to Entities.IDictionary
is a different animal in the sense that it is a mapping of unique keys to values. It is also enumerable in that you can enumerate the key/value pairs, but otherwise it serves a different purpose than the others you listed.The MSDN documentation for each of these is decent, so I would start there in rounding out your understanding.
我注意到gunny229的回答中存在一些问题。我在他的帖子的评论区提到了这些问题。后来,我想写一篇更详细的文章来连接一些缺失的点。
免责声明:我无意完全解决OP的问题,但我想指出使用LINQ时IQueryable和IEnumerable之间的区别SQL。
我在数据库中创建了以下结构(DDL 脚本):
这是记录插入脚本(DML 脚本):
现在我的目标是从数据库中的 Employee 表中获取前 2 条记录。我在 Visual Studio (VS) 中创建了一个新的 C# 控制台应用程序。然后,我添加了一个指向数据库中的 Employee 表的 ADO.NET 实体数据模型 XML (EDMX) 项。现在我可以开始编写 LINQ 查询了。
案例 I:IQueryable 路由的代码
在运行此程序之前,我已在 SQL Server 实例上启动了 SQL 查询分析器会话。以下是执行摘要:
触发的查询总数:1
查询文本:
我们可以看到 IQueryable 足够聪明,可以在数据库服务器端本身应用
Top (2)
子句。因此它只通过网络带来 5 条记录中的 2 条。客户端不再需要内存中过滤。案例 II: IEnumerable 路由的代码
此案例中的执行摘要:
触发的查询总数:1
SQL 探查器中捕获的查询文本:
现在需要注意的是,IEnumerable 带来了 Salary 表中存在的所有 5 条记录,然后在客户端执行内存中过滤获得前 2 条记录。因此,更多的数据(在本例中为 3 个附加记录)通过网络传输,并不必要地占用了带宽。
I noticed few issues in gunny229's answer. I've mentioned those issues in comment area of his post. Later on, I thought to write a more detailed post to connect some missing dots.
Disclaimer: I don't intend to cater OP's question in entirety but I want to point out the difference between IQueryable and IEnumerable when using LINQ to SQL.
I created following structure in DB (DDL script):
Here is the record insertion script (DML script):
Now my goal was to get top 2 records from Employee table in database. I created a new C# console application in Visual Studio (VS). Then, I added an ADO.NET Entity Data Model XML (EDMX) item pointing to Employee table in the database. Now I can start writing LINQ queries.
Case I: Code for IQueryable route
Before running this program, I had started a session of SQL Query profiler on the SQL Server instance. Here is the summary of execution:
Total number of queries fired: 1
Query text:
We can see that IQueryable is smart enough to apply the
Top (2)
clause on database server side itself. So it brings only 2 out of 5 records over the network. No more in-memory filtering is required on the client side.Case II: Code for IEnumerable route
Summary of execution in this case:
Total number of queries fired: 1
Query text captured in SQL profiler:
Now the thing to note is that IEnumerable brought all the 5 records present in Salary table and then performed an in-memory filteration on the client side to get top 2 records. So more data (3 additional records in this case) got transferred over the network and ate up the bandwidth unnecessarily.
IQueryable、IList、IDictionary、ICollection 继承 IEnumerable 接口。
所有集合类型的接口都会继承IEnumerable接口。
IEnumerable 和 IQueryable 之间的差异
IEnumerable:当您运行一个返回 IEnumerable 类型的查询时。
IEnumerable 将首先执行第一个查询,然后执行为该查询编写的子查询。
示例:如果您想获取特定国家/地区的前 10 条人口记录,那么我们将在 LINQ 中使用的查询是
Now,如果我们执行此查询,将首先执行第一个查询,IEnumerable 将获取以下记录:来自sql server的所有人口,然后它将数据存储在进程内存中,然后在下一个查询中执行前10个人口。(在sql server上执行两次)。
如果我们使用 IQueryable 执行相同的查询。
在此 IQueryable 中,它将同时执行两个查询,从某种意义上说,它将获取单个查询中前 10 名的人口,而不是获取数据并再次过滤前 10 名。(在 sql 服务器上执行一次) 。
IQueryable 和 IEnumerable 的结论
使用 IQueryable。
使用 IEnumerable。
IEnumerable 有一个方法 GetEnumerator()、Current() 和 MoveNext()。
IQueryable,IList,IDictionary,ICollection inherits IEnumerable Interface.
All the interfaces of type collection will inherits IEnumerable Interface.
Differences Between IEnumerable and IQueryable
IEnumerable: Wen you are running a query which returns of type IEnumerable.
IEnumerable will first Executes the first query and then executes the sub queries written for that query.
Example: If you want get the top 10 population Records from a Particular country then the query we will use in LINQ is
Now if we execute this query First Query will be executed first the IEnumerable will get the Records of all the population from the sql server then it will store the data in the In Process memory and then it executes the top 10 population in the next Query.(Execution is taken place for two times on sql server).
If we execute the same query by using IQueryable .
in this IQueryable it will execute the two Queries at the same time in the sense it will get the population which is of top 10 in a single Query instead of getting the data and filtering the first 10 again.(One time execution on sql server).
Conclusion for IQueryable and IEnumerable
use IQueryable.
use IEnumerable.
IEnumerable has a methods GetEnumerator(),Current() and MoveNext().