EF的ICollection VS名单VS IEnumerable的VS的IQueryable [英] EF ICollection Vs List Vs IEnumerable Vs IQueryable

查看:466
本文介绍了EF的ICollection VS名单VS IEnumerable的VS的IQueryable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我的EF模式有关系,根据我在实例所看到的,这些关系应该ICollection的虚拟属性来实现

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.

例如:

 public class Task
    {
        public int Id { get; set; }
        public string Description { get; set; }
        public virtual ICollection<SubTask>  { get; set; }
    }



我读的地方,我应该使用的IEnumerable防止延迟执行,是正确?这意味着,如果我的DAL方法返回的IEnumerable,IQueryable的仍然时,SQL会在那一刻执行,而不是当我打电话.TOList在网页的那一刻。

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的,名单?的IList,ICollection的?

So, what is the best practice? What should I return? IEnumerable, List?, IList, ICollection?

THX

推荐答案

的IQueryable


  • 不执行查询,直到你真正遍历项目,也许做一个 .ToList()的foreach 。这意味着你仍然可以添加过滤器,就像其中()

  • 扩展了IEnumerable

  • Query isn't executed until you really iterate over the items, maybe by doing a .ToList() or a foreach. Which means you still can add filters, like a Where().
  • Extends IEnumerable

的IEnumerable


  • 只进的产品清单。你不能没有经过项目0-3在项目4获得。

  • 只读列表中,您不能添加或从中删除它。

  • 仍可使用延迟执行(IQueryable的还是一个IEnumerable)。

的IList


  • 随机访问到完整列表

  • 可能完全在内存中(无延迟执行,但谁知道确切的类做什么实现这一点?)

  • 支持添加和删除

  • 扩展IEnumerable和ICollection的

  • 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

的ICollection


  • 是IEnumerable和IList的之间。

  • 扩展了IEnumerable

什么是最佳取决于您的要求。虽然通常一个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.

这篇关于EF的ICollection VS名单VS IEnumerable的VS的IQueryable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆