This code horrifies and scares me. I wrote this.
public IQueryable<StudentTrack> StudentTracks(int employeeId)
{
IQueryable<StudentTrack> EducationPortalStudentTracks =
from studentTrack in _dbContext.EducationPortalStudentTracks
join track in _dbContext.EducationPortalTracks on studentTrack.TrackId equals track.Id
orderby track.Id
where studentTrack.EmployeeId == employeeId
select new StudentTrack
{
Id = studentTrack.Id,
TrackId = studentTrack.TrackId,
Name = track.Name,
Deadline = studentTrack.Deadline,
// this is where it gets bad
PercentComplete = (from doesntMatter in _dbContext.EducationPortalStudentCourses
let courses = (from studentCourse in _dbContext.EducationPortalStudentCourses
join studentTrack_ in _dbContext.EducationPortalStudentTracks on studentCourse.TrackId equals studentTrack_.TrackId
where studentTrack.Id == studentTrack.Id && studentTrack.EmployeeId == studentCourse.EmployeeId
select studentCourse)
select courses.Where(x => x.Status == true).Count() * 100 / courses.Count()).FirstOrDefault()
};
return EducationPortalStudentTracks;
}
Let me explain how this fits in to the rest of the application. On the top level, a Kendo Grid control calls an MVC controller for its data. That controller calls another controller, which calls this. So this method returns the data which populates a grid in the UI. That grid lists all of the StudentTracks
in the database. In order to return the StudentTracks
, this method does a little bit of Linq and returns a series of StudentTrack
objects.
So the problem starts with the PercentComplete
field. To calculate the value for that field, we first find all of the children StudentCourses
. We then figure out how many of the StudentCourses
are complete (a.k.a. have True in the Status
column) and divide that by the total number of StudentCourses
.
Anyway, this is what I came up with. I'm not pleased with it, but I don't know how to do it any better. Any ideas on how I can make this a little more elegant?