C# EF ICollection Vs 列表 Vs IEnumerable Vs IQueryable
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11307171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
EF ICollection Vs List Vs IEnumerable Vs IQueryable
提问by Luis Valencia
so, my EF model has relationships and according to what I have seen in examples, those relationships should be done with virtual properties of ICollection.
所以,我的 EF 模型有关系,根据我在示例中看到的,这些关系应该使用 ICollection 的虚拟属性来完成。
Example:
例子:
public class Task
{
public int Id { get; set; }
public string Description { get; set; }
public virtual ICollection<SubTask> { get; set; }
}
I read somewhere that I should use IEnumerable to prevent deferred execution, is that correct? It means that if my DAL methods return IEnumerable, still of IQueryable, the SQL will be executed at that moment, and not at the moment when I call .TOList in the web page.
我在某处读到我应该使用 IEnumerable 来防止延迟执行,对吗?这意味着如果我的 DAL 方法返回 IEnumerable,仍然是 IQueryable,SQL 将在那个时刻执行,而不是在我在网页中调用 .TOList 的那一刻。
So, what is the best practice? What should I return? IEnumerable, List?, IList, ICollection?
那么,最佳实践是什么?我应该返回什么?IEnumerable, List?, IList, ICollection?
thx
谢谢
采纳答案by Hans Ke?ing
- Query isn't executed until you really iterate over the items, maybe by doing a
.ToList()or aforeach. Which means you still can add filters, like aWhere(). - Extends IEnumerable
- 直到您真正遍历项目(可能通过执行 a
.ToList()或 a )后,才会执行查询foreach。这意味着您仍然可以添加过滤器,例如Where(). - 扩展 IEnumerable
- Forward-only list of items. You can't get at "item 4" without passing items 0-3.
- Read-only list, you can't add to it or remove from it.
- Still might use deferred execution (IQueryable is still an IEnumerable).
- 仅向前的项目列表。不通过第 0-3 项就无法到达“第 4 项”。
- 只读列表,您无法向其中添加或从中删除。
- 仍然可能使用延迟执行(IQueryable 仍然是一个 IEnumerable)。
- Random access to the full list
- Probably entirely in memory (no deferred execution, but who knows what the exact class does that implements this?)
- Supports adding and removing
- Extends IEnumerable and ICollection
- 随机获取完整列表
- 可能完全在内存中(没有延迟执行,但谁知道实现这个的确切类是什么?)
- 支持添加和删除
- 扩展 IEnumerable 和 ICollection
- Is between IEnumerable and IList.
- Extends IEnumerable
- 介于 IEnumerable 和 IList 之间。
- 扩展 IEnumerable
What is "best" depends on your requirements. Usually though an IEnumerable is "good enough" if you only want to display items. At least always use the generic variant.
什么是“最佳”取决于您的要求。通常,如果您只想显示项目,IEnumerable 就“足够好”了。至少总是使用通用变体。
回答by Mark
Another difference in case of EF is that the IEnumerable is not executing the query to the DB untill you really enumerate the list. While the IList will execute the query and fetch the items in memory. I had a strange behaviour with caching. Chaching the IEnumarable didn't store the items but rather the unfetched enumeration, everytime i would call the cache and fetch the enumeration for some purpose, it would go to the DB and execute the query, so caching was useless. But a ToList() on the enumeration fetched the items and stored the items in the cache, so only 1 trip to the DB for this. Find more in this post: http://www.dailycode.info/BlogV2/post/entityframework-adding-ienumerable-to-cache
EF 的另一个区别是 IEnumerable 不会执行对数据库的查询,直到您真正枚举列表。而 IList 将执行查询并获取内存中的项目。我有一个奇怪的缓存行为。缓存 IEnumarable 不存储项目,而是存储未获取的枚举,每次我调用缓存并出于某种目的获取枚举时,它都会转到数据库并执行查询,因此缓存是无用的。但是枚举上的 ToList() 获取了项目并将这些项目存储在缓存中,因此只有 1 次访问数据库。在这篇文章中找到更多信息:http: //www.dailycode.info/BlogV2/post/entityframework-adding-ienumerable-to-cache

