有人对在 Linq to Entities 中计算百分比有任何提示吗?
我猜肯定有比返回 2 个结果并在内存中计算更有效的方法。也许是对 let 或 into 的创造性使用?
编辑
感谢 Mark 的评论,这是一个代码片段,但我认为这将导致 2 个数据库命中:
int passed = (from lpt in this.PushedLearnings.Select(pl => pl.LearningPlanTask)
where lpt.OnlineCourseScores.Any(score => score.ActualScore >= ((lpt.LearningResource.PassMarkPercentage != (decimal?)null) ?lpt.LearningResource.PassMarkPercentage : 80))
select lpt).Count();
int total = (from lpt in this.PushedLearnings.Select(pl => pl.LearningPlanTask)
select lpt).Count();
double percentage = passed * 100 / total;