我有一张表格,记录了人员开始项目的时间:
PersonId
ProjectId
StartDate
我希望使用linq(linq-to-entities)来获取像这样的结果集。
PersonId
ProjectId
StartDate
EndDate
当按照开始日期排序时,EndDate代表他们下一个项目的开始日期,如果没有更近的项目,则为null。
这是我所做的:
context.PersonProjects.Select(pp => new {
pp.PersonId,
pp.ProjectId,
pp.StartDate,
EndDate = context.PersonProjects.Where(pp2 => pp2.PersonId == pp.PersonId && pp2.StartDate > pp.StartDate).OrderBy(pp2 => pp2.StartDate).Select(pp2 => pp2.StartDate).FirstOrDefault()
})
这样做有更好的性能优化方法吗?
IQueryable
,并且只有在所需子集在内存中时才执行修复部分。 - Sergey KalinichenkoEndDate
存储在数据库中。这会增加一些维护工作,但大大简化了查询,并且还可以让你直接在数据库中执行非常高效的“截止日期”查询。 - Sergey Kalinichenko