Linq到实体百分比

Linq到实体百分比,linq,linq-to-entities,percentage,Linq,Linq To Entities,Percentage,有人有没有关于计算Linq中实体百分比的技巧 我猜一定有比返回两个结果并在内存中计算更有效的方法。也许是let或into的创造性使用 编辑 感谢Mark的评论,这是一段代码片段,但我认为这将导致2次数据库点击: int passed = (from lpt in this.PushedLearnings.Select(pl => pl.LearningPlanTask) where lpt.OnlineCourseScores.Any(score => score.ActualSc

有人有没有关于计算Linq中实体百分比的技巧

我猜一定有比返回两个结果并在内存中计算更有效的方法。也许是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;

如果在查询中使用LINQ to Entities并沿
select x*100.0/y
行写入内容,则此表达式将转换为SQL并在数据库中运行。这会很有效率。

你能发布你的代码吗?谢谢@MarkByers,我在一个代码示例中弹出了一个。