public async Task <ActionResult> SelectCourse([FromBody] SelectCourseDto dto) { var course = await _courseRepository.LoadEntitiesAsIQueryable(x => x.CourseId == dto.CourseId).FirstOrDefaultAsync(); if (course == null) { return(BadRequest()); } var students = await _studentRepository.LoadEntitiesAsIQueryable(x => x.ClassId == dto.ClassId).ToListAsync(); foreach (var item in students) { StudentCourse sc = new StudentCourse { Course = course, Student = item, }; var dbsc = await _studentCourseRepository .LoadEntitiesAsIQueryable(x => x.CourseId == course.CourseId && x.StudentId == item.StudentId) .FirstOrDefaultAsync(); if (dbsc != null) { return(BadRequest()); } await _studentCourseRepository.AddEntityAsync(sc); } return(NoContent()); }