0

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.

JonDog
  • 517
  • 7
  • 23
  • possibly duplicate of http://stackoverflow.com/questions/4474951/entity-framework-include-is-not-working/4475504#4475504 , in short words, `Include` is ignored when using with projection (select). – Hopeless Aug 27 '15 at 01:35
  • Also there is a work-around here http://blogs.msdn.com/b/alexj/archive/2009/06/02/tip-22-how-to-make-include-really-include.aspx , by using `Include` at the end after projection, I think it should also work in your case (which looks more complicated with group by). – Hopeless Aug 27 '15 at 01:39

0 Answers0