// -DtoQuery

        public async Task <StudentCourseDto> CreateStudentCourseAsync(StudentCourseDto studentCourseDto, string username)
        {
            OnCreate(studentCourseDto, username);

            var entity = StudentCourseDto.AsStudentCourseFunc(studentCourseDto);

            ToEntity(ref entity, studentCourseDto);
            //entity.InsertUser = entity.LastActivityUser = username;
            //entity.InsertDateTime = entity.LastActivityDateTime = DateTime.UtcNow;
            entity.AddTracker(username);

            _context.StudentCourses.Add(entity);

            OnBeforeCreate(entity, username);
            try
            {
                await _context.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                // _context.Entry(entity).State = EntityState.Detached;
                throw new Exception("Add error", ex);
            }
            finally
            {
                // _context.Entry(entity).State = EntityState.Detached;
            }
            OnAfterCreate(entity, username);

            // studentCourseDto = StudentCourseDto.AsStudentCourseDtoFunc(entity);
            studentCourseDto = await GetStudentCourseDtoAsync(entity.Id, StudentCourseDto.IncludeNavigations());

            return(studentCourseDto);
        }
        public async Task <bool> UpdateStudentCourseAsync(StudentCourseDto studentCourseDto, string username /*, String[] includeNavigations, params Expression<Func<StudentCourse, bool>>[] filters*/)
        {
            OnUpdate(studentCourseDto, username);

            // Get StudentCourse
            var entity = EntityQuery(_context, StudentCourseDto.IncludeNavigations())
                         .FirstOrDefault(x => x.Id == studentCourseDto.Id);

            if (entity != null)
            {
                entity = StudentCourseDto.ToStudentCourseFunc(entity, studentCourseDto);

                ToEntity(ref entity, studentCourseDto);
                //entity.UpdateUser = entity.LastActivityUser = username;
                //entity.UpdateDateTime = entity.LastActivityDateTime = DateTime.UtcNow;
                entity.EditTracker(username);

                OnBeforeUpdate(entity, username);
                try
                {
                    await _context.SaveChangesAsync();
                }
                catch (Exception ex)
                {
                    // _context.Entry(entity).State = EntityState.Detached;
                    throw new Exception("Update error", ex);
                }
                finally
                {
                    // _context.Entry(entity).State = EntityState.Detached;
                }
                OnAfterUpdate(entity, username);
            }
            else
            {
                return(false);
            }

            return(true);
        }
        public async Task <ActionResult <StudentCourseDto> > GetStudentCourse(int id)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var studentCourse = await studentCourseService.GetStudentCourseDtoAsync(id, StudentCourseDto.IncludeNavigations());

            if (studentCourse == null)
            {
                return(NotFound());
            }

            return(studentCourse);
        }
        public async Task <ActionResult <IEnumerable <StudentCourseDto> > > GetStudentCourses(string searchText = null
                                                                                              // +
                                                                                              , int?sessionId      = null
                                                                                              , string sessionText = null

                                                                                              // -
                                                                                              // +
                                                                                              , int?semesterId      = null
                                                                                              , string semesterText = null

                                                                                              // -
                                                                                              // +
                                                                                              , int?studentId      = null
                                                                                              , string studentText = null

                                                                                              // -
                                                                                              // +
                                                                                              , int?courseId      = null
                                                                                              , string courseText = null

                                                                                              // -
                                                                                              , double?scoreText = null
                                                                                              /*, int pageNumber=1, int pageSize=7*/)
        {
            // var studentCourses = _context.StudentCourses.Select(StudentCourseDto.AsStudentCourseDto);
            List <Expression <Func <StudentCourseDto, bool> > > filters = null;

            if (String.IsNullOrEmpty(searchText)
                // +
                && (sessionId is null) &&
                String.IsNullOrEmpty(sessionText)
                // -
                // +
                && (semesterId is null) &&
                String.IsNullOrEmpty(semesterText)
                // -
                // +
                && (studentId is null) &&
                String.IsNullOrEmpty(studentText)
                // -
                // +
                && (courseId is null) &&
                String.IsNullOrEmpty(courseText)
                // -
                && scoreText == null
                )
            {
                // return null;
            }
            else
            {
                filters = new List <Expression <Func <StudentCourseDto, bool> > >();

                if (!String.IsNullOrEmpty(searchText))
                {
                    if (searchText.CompareTo("*") != 0 && searchText.CompareTo("%") != 0)
                    {
                        filters.Add(x => x.Id.ToString().Contains(searchText));
                    }
                }
                // +
                if (!(sessionId is null))
                {
                    filters.Add(x => x.SessionId == sessionId);
                }
                if (!String.IsNullOrEmpty(sessionText))
                {
                    filters.Add(x => x.SessionName == sessionText);
                }

                // -
                // +
                if (!(semesterId is null))
                {
                    filters.Add(x => x.SemesterId == semesterId);
                }
                if (!String.IsNullOrEmpty(semesterText))
                {
                    filters.Add(x => x.SemesterName == semesterText);
                }

                // -
                // +
                if (!(studentId is null))
                {
                    filters.Add(x => x.StudentId == studentId);
                }
                if (!String.IsNullOrEmpty(studentText))
                {
                    filters.Add(x => x.StudentMatricNo == studentText);
                }

                // -
                // +
                if (!(courseId is null))
                {
                    filters.Add(x => x.CourseId == courseId);
                }
                if (!String.IsNullOrEmpty(courseText))
                {
                    filters.Add(x => x.CourseCode == courseText);
                }

                // -
                if (scoreText != null)
                {
                    filters.Add(x => x.Score == scoreText);
                }
            }

            //sort
            //return studentCourses.OrderBy(o => o.Id).Skip(((pageNumber - 1) * pageSize)).Take(pageSize);

            // OnSelectQuery(ref studentCourses);

            // return await studentCourses.ToListAsync();

            if (filters == null)
            {
                return(await studentCourseService.GetStudentCourseDtoesAsync(StudentCourseDto.IncludeNavigations()));
            }
            else
            {
                return(await studentCourseService.GetStudentCourseDtoesAsync(StudentCourseDto.IncludeNavigations(), filters.ToArray()));
            }
        }
        public async Task <ActionResult <IEnumerable <StudentCourseDto> > > GetStudentCourses(string searchText = null
                                                                                              , int?sessionId   = null
                                                                                              , int?semesterId  = null
                                                                                              , int?studentId   = null
                                                                                              , int?courseId    = null
                                                                                              , double?score    = null
                                                                                                                                 // +navigation
                                                                                                                                 // +Student
                                                                                              , string studentMatricNo    = null
                                                                                              , string studentDescription = null //Basic-Nav-Property
                                                                                                                                 // -Student
                                                                                                                                 // +Course
                                                                                              , string courseCode        = null
                                                                                              , string courseDescription = null  //Basic-Nav-Property
                                                                                                                                 // -Course
                                                                                                                                 // +Session
                                                                                              , string sessionName = null
                                                                                                                                 // -Session
                                                                                                                                 // +Semester
                                                                                              , string semesterName = null
                                                                                                                                 // -Semester
                                                                                                                                 // -navigation
                                                                                              /*, int pageNumber=1, int pageSize=7*/)
        {
            // var studentCourses = _context.StudentCourses.Select(StudentCourseDto.AsStudentCourseDto);
            List <Expression <Func <StudentCourseDto, bool> > > filters = null;

            if (String.IsNullOrEmpty(searchText) &&
                (sessionId == null) &&
                (semesterId == null) &&
                (studentId == null) &&
                (courseId == null) &&
                (score == null)
                // +navigation
                // +Student
                && (String.IsNullOrEmpty(studentMatricNo)) &&
                (String.IsNullOrEmpty(studentDescription)) //Basic-Nav-Property
                // -Student
                // +Course
                && (String.IsNullOrEmpty(courseCode)) &&
                (String.IsNullOrEmpty(courseDescription)) //Basic-Nav-Property
                // -Course
                // +Session
                && (String.IsNullOrEmpty(sessionName))
                // -Session
                // +Semester
                && (String.IsNullOrEmpty(semesterName))
                // -Semester
                // -navigation

                )
            {
                // return null;
            }
            else
            {
                filters = new List <Expression <Func <StudentCourseDto, bool> > >();

                if (!String.IsNullOrEmpty(searchText))
                {
                    if (searchText.CompareTo("*") != 0 && searchText.CompareTo("%") != 0)
                    {
                        filters.Add(x => x.Id.ToString().Contains(searchText));
                    }
                }

                if (sessionId != null)
                {
                    filters.Add(x => x.SessionId == sessionId);
                }

                if (semesterId != null)
                {
                    filters.Add(x => x.SemesterId == semesterId);
                }

                if (studentId != null)
                {
                    filters.Add(x => x.StudentId == studentId);
                }

                if (courseId != null)
                {
                    filters.Add(x => x.CourseId == courseId);
                }

                if (score != null)
                {
                    filters.Add(x => x.Score == score);
                }
                // +navigation
                // +Student

                if (!String.IsNullOrEmpty(studentMatricNo))
                {
                    filters.Add(x => x.StudentMatricNo == studentMatricNo);
                }

                if (!String.IsNullOrEmpty(studentDescription))
                {
                    filters.Add(x => x.StudentDescription == studentDescription);
                } //Basic-Nav-Property
                // -Student
                // +Course

                if (!String.IsNullOrEmpty(courseCode))
                {
                    filters.Add(x => x.CourseCode == courseCode);
                }

                if (!String.IsNullOrEmpty(courseDescription))
                {
                    filters.Add(x => x.CourseDescription == courseDescription);
                } //Basic-Nav-Property
                // -Course
                // +Session

                if (!String.IsNullOrEmpty(sessionName))
                {
                    filters.Add(x => x.SessionName == sessionName);
                }
                // -Session
                // +Semester

                if (!String.IsNullOrEmpty(semesterName))
                {
                    filters.Add(x => x.SemesterName == semesterName);
                }
                // -Semester
                // -navigation
            }

            //sort
            //return studentCourses.OrderBy(o => o.Id).Skip(((pageNumber - 1) * pageSize)).Take(pageSize);

            // OnSelectQuery(ref studentCourses);

            // return await studentCourses.ToListAsync();

            if (filters == null)
            {
                return(await studentCourseService.GetStudentCourseDtoesAsync(StudentCourseDto.IncludeNavigations()));
            }
            else
            {
                return(await studentCourseService.GetStudentCourseDtoesAsync(StudentCourseDto.IncludeNavigations(), filters.ToArray()));
            }
        }