我正在尝试使用急切加载导航属性来创建查询。有没有办法加载导航属性的属性?当我尝试下面的代码时,我得到一个“序列不包含元素”异常。
var viewModel = new InstructorIndexData();
viewModel.Instructors = db.Instructors.Include(i => i.OfficeAssignment)
.Include(i=>i.Assignments)
.Include(i => i.Students)
.Include (i=>i.Enrollments )
.Include(i => i.Courses.Select(c => c.Department))
.OrderBy(i => i.LastName);
viewModel.Courses = viewModel.Instructors.Where(i => i.InstructorID == id).Single().Courses;
viewModel.Enrollments = viewModel.Courses.Where(x => x.CourseID == courseID).Where( e=>e.Enrollments.First().InstructorFullName == "Kim Abercrombie").Single().Enrollments;
但是,当我使用:
viewModel.Enrollments = viewModel.Courses.Where(x => x.CourseID == courseID).Single().Enrollments;
它很好地填充了我的 viewModel.Enrollment 属性。有没有办法在 linq 查询中访问这种类型的复杂属性?