Exemplo n.º 1
0
        // GET: Instructors
        public ActionResult Index(int?id, int?courseID)     //id --- instructor
        {
            var viewModel = new InstructorsData();

            //Query
            //inner join
            //Eager loading
            viewModel.Instructors = db.Instructors
                                    .Include(i => i.OfficeAssignment)
                                    //.Include(i => i.Courses.Select(c => c.Department))-----navigate mesw ths class course na parw to Department
                                    .OrderBy(i => i.LastName);

            if (id != null)
            {
                ViewBag.InstructorID = id.Value;
                viewModel.Courses    = viewModel.Instructors                           //Ta Courses Tha ta ferei logw "Lazy-Loading" epeidh tou zhththikan
                                       .Where(i => i.ID == id.Value).Single().Courses; //single tha ferei ena instructors apo th list viewmodel.Instuctors pou epilexame
                //tha gemisei thn list apo courses gia enan Instructors
            }

            if (courseID != null)
            {
                ViewBag.CourseID = courseID.Value;
                //Lazy Loading
                viewModel.Enrollments = viewModel.Courses   //Ta Enrollments Tha ta ferei logw "Lazy-Loading" epeidh tou zhththikan
                                        .Where(c => c.CourseID == courseID).Single().Enrollments;

                ////1h epilogh Lazy
                //2h Eager
                //Advanced texnikh Explicit
                //Explicit Loading----eite collection(lexh)eite load(lexh)---ayta pou xreiazomai akrivws----

                //var selectedCourse = viewModel.Courses
                //    .Where(x => x.CourseID == courseID).Single();
                //db.Entry(selectedCourse).Collection(x => x.Enrollments).Load(); //collection ta polla gia ena to load
                //foreach (Enrollment enrollment in selectedCourse.Enrollments)
                //{
                //    db.Entry(enrollment).Reference(x => x.Student).Load();  //reference ena tha parw
                //}
                //viewModel.Enrollments = selectedCourse.Enrollments;
            }

            return(View(viewModel));
        }
Exemplo n.º 2
0
        // GET: Instructors
        public ActionResult Index(int?id, int?courseID)
        {
            var viewModel = new InstructorsData();

            // eager Loading
            viewModel.Instructors = db.Instructors
                                    .Include(i => i.OfficeAssignment)
                                    //.(i => i.Courses.Select(c => c.Department))
                                    .OrderBy(i => i.Lastname);

            if (id != null)
            {
                ViewBag.InstructorID = id.Value;
                viewModel.Courses    = viewModel.Instructors
                                       .Where(i => i.ID == id.Value).Single().Courses;
            }

            if (courseID != null)
            {
                ViewBag.CourseID = courseID.Value;
                // Lazy Loading
                viewModel.Enrollments = viewModel.Courses
                                        .Where(c => c.CourseID == courseID).Single().Enrollments;
                // Explicit Loading
                //var selectedCourse = viewModel.Courses
                //    .Where(x => x.CourseID == courseID).Single();
                //db.Entry(selectedCourse).Collection(x => x.Enrollments).Load();
                //foreach (Enrollment enrollment in selectedCourse.Enrollments)
                //{
                //    db.Entry(enrollment).Reference(x => x.Student).Load();
                //}
                //viewModel.Enrollments = selectedCourse.Enrollments;
            }

            return(View(viewModel));
        }