I'm using a linq query which looks (after some simplification) something like the following:
List<UserExams> listUserExams = GetUserExams();
var examData =
from userExam in listUserExams
group by userExam.ExamID into groupExams
select new ExamData()
{
ExamID = groupExams.Key,
AverageGrade = groupExams.Average(e => e.Grade),
PassedUsersNum = groupExams.Count(e => /* Some long and complicated calculation */),
CompletionRate = 100 * groupExams.Count(e => /* The same long and complicated calculation */) / TotalUsersNum
};
What bothers me is the calculation expression which appears twice, for PassedUsersNum and CompletionRate.
Assuming that CompletionRate = (PassedUsersNum / TotalUsersNum) * 100
, how can I write it by reusing the calculation of PassedUsersNum, instead of writing that expression again?