I am querying a repository and using AllIncluding to add other repos into my query. With the query below I get all enrollments, calendars, students, and schools just as expected.

            var allStudentEnrollments = uow.EnrollmentRepo
                                        .AllIncluding(i => i.Calendar, i => i.Student, i => i.Calendar.School)
                                        .Where(w => w.Calendar.School.District.Project.ProjectId == CurrentProjectId &&
                                                    (w.Grade == "01"||
                                                    w.Grade == "02"));

What I would like to do now is get the most recent enrollment. I have tried to do this by ordering by startdate and then selecting the first record but I seem to loose all of my additional data (student, calendar, school). The query below is the what I have started to work on but can seem to get it to work.

            IQueryable<StudentCalendar> students = uow.StudentCalendarRepo
                                                  .AllIncluding(i => i.Calendar, i => i.Student, i => i.Calendar.School)
                                                  .Where(w => w.Calendar.School.District.Project.ProjectId == CurrentProjectId &&
                                                              (w.Grade == CurrentCohortTrack.LowerGradeLevel ||
                                                               w.Grade == CurrentCohortTrack.UpperGradeLevel))
                                                  .GroupBy(g => g.StudentId)
                                                  .Select(s => s.OrderByDescending(o => o.EnrolledStartDate)
                                                                .FirstOrDefault());

Can anyone me how to get the expected results? Any help would be well appreciated.

Related posts

Recent Viewed