public async Task <IActionResult> Index(int?id, int?courseId)
        {
            var viewModel = new InstructorIndex();

            viewModel.Instructors = await _context.Instructors
                                    .Include(i => i.OfficeAssignment)
                                    // These 4 lines are unncessary when using Explicit Loading (below)
                                    //.Include(i => i.CourseAssignments)
                                    //.ThenInclude(i => i.Course)
                                    //    .ThenInclude(i => i.Enrollments)
                                    //        .ThenInclude(i => i.Student)
                                    .Include(i => i.CourseAssignments)
                                    .ThenInclude(i => i.Course)
                                    .ThenInclude(i => i.Department)
                                    //.AsNoTracking() // Use AsNoTracking with Eager Loading, not with Explicit Loading (below)
                                    .OrderBy(i => i.LastName)
                                    .ToListAsync();

            if (id != null)
            {
                ViewData["Id"] = id.Value;
                Instructor instructor = viewModel.Instructors.Where(
                    i => i.Id == id.Value).Single();
                viewModel.Courses = instructor.CourseAssignments.Select(s => s.Course);
            }

            // Eager Loading or Explicit Loading (below)
            //if (courseId != null)
            //{
            //    ViewData["CourseId"] = courseId.Value;
            //    viewModel.Enrollments = viewModel.Courses.Where(
            //        x => x.Id == courseId).Single()
            //        .Enrollments;
            //}

            // Explicit Loading instead of Eager Loading (above)
            if (courseId != null)
            {
                ViewData["CourseId"] = courseId.Value;
                var selectedCourse = viewModel.Courses.Where(x => x.Id == courseId).Single();
                await _context.Entry(selectedCourse).Collection(x => x.Enrollments).LoadAsync();

                foreach (Enrollment enrollment in selectedCourse.Enrollments)
                {
                    await _context.Entry(enrollment).Reference(x => x.Student).LoadAsync();
                }
                viewModel.Enrollments = selectedCourse.Enrollments;
            }

            return(View(viewModel));
        }
        // GET: Instructors
        public async Task <IActionResult> Index(int?id, int?courseId)
        {
            var viewModel = new InstructorIndex();

            viewModel.Instructors = await _context.Instructors
                                    .Include(i => i.OfficeAssignment)
                                    .Include(i => i.CourseAssignments)
                                    //.ThenInclude(i => i.Course)
                                    //    .ThenInclude(i => i.Enrollments)
                                    //    .ThenInclude(i => i.Student)
                                    .Include(i => i.CourseAssignments)
                                    .ThenInclude(i => i.Course)
                                    .ThenInclude(i => i.Department)
                                    //.AsNoTracking()
                                    .OrderBy(i => i.LastName)
                                    .ToListAsync();

            if (id != null)
            {
                ViewData["InstructorID"] = id.Value;
                Instructor instructor = viewModel.Instructors.Where
                                            (i => i.ID == id.Value).Single();
                viewModel.Courses = instructor.CourseAssignments.Select(s => s.Course);
            }

            if (courseId != null)
            {
                ViewData["CourseID"] = courseId.Value;
                //viewModel.Enrollments = viewModel.Courses.Where  // based on the courseID being passed into the Index() method returns a list of IEnumerable<Enrollment>
                //      (x => x.CourseID == courseId).Single().Enrollments;  // return a single course with Enrollments

                var selectedCourse = viewModel
                                     .Courses
                                     .Where(x => x.CourseID == courseId)
                                     .Single();
                await _context.Entry(selectedCourse).Collection(x => x.Enrollments).LoadAsync();

                foreach (Enrollment enrollment in selectedCourse.Enrollments)
                {
                    await _context.Entry(enrollment).Reference(s => s.Student).LoadAsync();
                }
                viewModel.Enrollments = selectedCourse.Enrollments;
            }

            return(View(viewModel));
        }