我有像Property一样的嵌套实体 - >设施 - >农场 - >许可证等。
我想获得最新的许可证,我的原始查询看起来像这样。
return _db.Properties
.Include(c => c.Contact)
.Include(f => f.Facilities.Select(c => c.Contact))
.Include(f => f.Facilities.Select(ff => ff.Farms.Select(s => s.Species)))
.Include(x => x.Facilities.Select(l => l.Licenses))
.SingleOrDefault(x => x.PropertyID == id);
因为我想获得最近的许可证,我试过
return _db.Properties
.Include(c => c.Contact)
.Include(f => f.Facilities.Select(c => c.Contact))
.Include(f => f.Facilities.Select(ff => ff.Farms.Select(s => s.Species)))
.Include(x => x.Facilities.Select(l => l.Licenses.OrderByDescending(d => d.IssueDate)))
.SingleOrDefault(x => x.PropertyID == id);
当然因为你不能在那里使用orderby子句而失败。
如果我在linq的末尾尝试orderby子句,它将从Property开始,我将需要再次向下钻取许可证。
这是什么首选惯例?
- EDIT-- 根据罗伯特的建议,我翻了一下查询以获得许可证,但我想我需要做一些预测,因为这个查询只给了Property实体,没有子实体。
return _db.Licenses.OrderByDescending(x => x.IssueDate)
.Include(f => f.Facility)
.Include(f => f.Facility.Property)
.Include(f => f.Facility.Property.Contact)
.Include(c => c.Facility.Contact)
.Include(f => f.Facility.Farms)
.Include(f => f.Facility.Farms.Select(s => s.Species))
.Where(x => x.Facility.Property.PropertyID == id)
.Select(x => x.Facility.Property).SingleOrDefault();
任何想要让所有孩子接受的想法?