2012-06-18 165 views
0

是否有人計算百分比在LINQ到實體任何提示?LINQ到實體百分比

我猜一定有比返回2周的結果,並在內存計算更有效的方式。也許創造性地使用let或into?

編輯


感謝馬克的評論,這裏是一個代碼片段,但我認爲這將導致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; 
+1

你可以發佈你的代碼? –

+0

感謝@MarkByers我在代碼示例中彈出。 –

回答

2

如果使用LINQ到實體,寫在你的查詢中沿着select x * 100.0/y行的東西,然後這個表達式將被轉換爲SQL並在數據庫中運行。這將是有效的。