public async Task OnGetAsync(int?id, int?courseID)
        {
            int pageIndex = 0;
            int pageSize  = 3;

            string searchField = string.Empty;
            string searchQuery = string.Empty;
            bool   searchMode  = false;

            ////////////////////////////////////////////////////////////////////////////////////

            if (!String.IsNullOrEmpty(Request.Query["SearchField"]) &&
                !String.IsNullOrEmpty(Request.Query["SearchQuery"]))
            {
                searchMode  = true;
                searchField = Request.Query["SearchField"];
                searchQuery = Request.Query["SearchQuery"];
            }

            searchingVar             = new SearchingVar();
            searchingVar.SearchMode  = searchMode;
            searchingVar.SearchField = searchField;
            searchingVar.SearchQuery = searchQuery;
            searchingVar.DicField    = new Dictionary <string, string>()
            {
                { "LastName", "성" },
                { "FirstMidName", "이름" },
                { "Name", "전체" }
            };

            //////////////////////////////////////////////////////////////////////////////


            Instructor = new InstructorIndexData();
            var iqInstructor = (IQueryable <Instructor>)_context.Instructors
                               .Include(i => i.OfficeAssignment)
                               .Include(i => i.CourseAssignments)
                               .ThenInclude(c => c.Course)
                               .ThenInclude(c => c.Department);

            //.Include(i => i.CourseAssignments)
            //    .ThenInclude(c => c.Course)
            //        .ThenInclude(c => c.Enrollments)
            //            .ThenInclude(e => e.Student);

            if (searchMode)
            {
                switch (searchField)
                {
                case "LastName":
                    iqInstructor = iqInstructor.Where(s => s.LastName.Contains(searchQuery));
                    break;

                case "FirstMidName":
                    iqInstructor = iqInstructor.Where(s => s.FirstMidName.Contains(searchQuery));
                    break;

                default:
                    iqInstructor = iqInstructor.Where(s => s.FirstMidName.Contains(searchQuery) || s.LastName.Contains(searchQuery));
                    break;
                }
            }

            iqInstructor = iqInstructor.OrderByDescending(i => i.ID);



            //[1] 쿼리스트링에 따른 페이지 보여주기
            if (!string.IsNullOrEmpty(Request.Query["Page"]))
            {
                // Page는 보여지는 쪽은 1, 2, 3, ... 코드단에서는 0, 1, 2, ...
                pageIndex = Convert.ToInt32(Request.Query["Page"]) - 1;
                //Response.Cookies.Append("Page", pageIndex.ToString());
            }

            TotalCount = iqInstructor.Count();
            PageIndex  = pageIndex + 1;

            Instructor.Instructors = await iqInstructor
                                     .Skip((pageIndex) *pageSize).Take(pageSize).ToListAsync();


            if (id != null)
            {
                InstructorID = id.Value;
                var inss = Instructor.Instructors.Where(i => i.ID == id.Value).Single();
                Instructor.Courses = inss.CourseAssignments.Select(c => c.Course);
            }

            if (courseID != null)
            {
                CourseID = courseID.Value;

                //Instructor.Enrollments =_context.Courses
                //    .Include(c=>c.Enrollments)
                //        .ThenInclude(e=>e.Student)
                //     .Where(x => x.CourseID == courseID).Single().Enrollments;

                var selectedCourse = Instructor.Courses.SingleOrDefault(c => c.CourseID == courseID);
                await _context.Entry(selectedCourse).Collection(x => x.Enrollments).LoadAsync();

                foreach (var item in selectedCourse.Enrollments)
                {
                    await _context.Entry(item).Reference(x => x.Student).LoadAsync();
                }
                Instructor.Enrollments = selectedCourse.Enrollments;
            }
        }
Ejemplo n.º 2
0
        public async Task OnGetAsync(int?id, int?courseID)
        {
            Instructor = new InstructorIndexData();

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


            if (id != null)
            {
                InstructorID = id.Value;

                var seletedInstructor = Instructor.Instructors.Where(i => i.ID == id).Single();

                //1. Load를 이용한 별도 쿼리
                //await _context.CourseAssignments.Where(c => c.InstructorID == seletedInstructor.ID).LoadAsync();

                //foreach (var item in seletedInstructor.CourseAssignments)
                //{
                //    await _context.Courses.Include(c => c.Department).Where(c => c.CourseID == item.CourseID).LoadAsync();
                //}


                //2. 명시적 로드
                await _context.Entry(seletedInstructor).Collection(x => x.CourseAssignments).LoadAsync();

                foreach (var item in seletedInstructor.CourseAssignments)
                {
                    await _context.Entry(item).Reference(x => x.Course).LoadAsync();

                    await _context.Entry(item.Course).Reference(x => x.Department).LoadAsync();
                }

                Instructor.Courses = seletedInstructor.CourseAssignments.Select(c => c.Course);
            }

            if (courseID != null)
            {
                CourseID = courseID.Value;


                //1. 즉시로드
                //Instructor.Enrollments = Instructor.Courses.Where(c => c.CourseID == courseID).Single().Enrollments;

                //2. 별도로드
                var selectedCourse = Instructor.Courses.Where(x => x.CourseID == courseID).Single();
                await _context.Enrollments.Include(e => e.Student).Where(e => e.CourseID == selectedCourse.CourseID).LoadAsync();

                //foreach (var item in selectedCourse.Enrollments)
                //{
                //    await _context.Students.Where(s => s.ID == item.StudentID).LoadAsync();
                //}

                Instructor.Enrollments = selectedCourse.Enrollments;


                //3.명시적 로드
                //var selectedCourse = Instructor.Courses.Where(x => x.CourseID == courseID).Single();
                //await _context.Entry(selectedCourse).Collection(x => x.Enrollments).LoadAsync();
                //foreach (var enrollment in selectedCourse.Enrollments)
                //{
                //    await _context.Entry(enrollment).Reference(x => x.Student).LoadAsync();

                //}
                //Instructor.Enrollments = selectedCourse.Enrollments;
            }
        }