【发布时间】:2013-12-02 15:28:41
【问题描述】:
当想要使用 EF 导航(通过类的属性导航)时,List<T>´s 都在内存中处理。
例如我有这个 EF 模型类:
class School
{
public virtual ICollection<Groups> Groups { get; set; }
...
public School()
{
this.Courses = new List<Group>(); // List<T>!!
}
}
如果我这样做:
someSchool.Groups.Count
我将计算内存中的组而不是 SQL(即:这些不会像 "select count(*) from Groups join School Where SchoolId = ..." 那样计算)
所以我的问题是.. 我应该使用什么来代替 List?
IEnumerable 是一个接口,所以我不能有一个新的 IEnumerable,... IQueryable 也是..
如果没有适合于此的集合类,那么我想我应该使用我的 DbContext 实例。像这样:
(new MyDbContext()).Groups.Count(g => g.SchoolId == ...)
如果是这样,那么:为什么会有EF导航?!??
编辑:
好吧,也许我应该使用真实信息:
我已经在使用 ICollection(我在帖子中使用了 IEnumerable,因为我认为它们相似)
这是慢查询:
domain.Persons.Count(p => p.IsStudent && p.GuardianId != null && p.Guardian.Mobile.Equals(""))这是快速查询:
db.Persons.Count(p => p.Domains.Any(d => d.DomainId == domain.DomainId) && p.IsStudent && p.GuardianId != null && p.Guardian.Mobile.Equals(""))
如您所见,2 和 3 非常相似……一个使用导航,另一个不使用。
【问题讨论】:
标签: entity-framework