public async Task <IActionResult> CreateConfirm(int?groupId, int?disciplineId) { if (groupId == null || disciplineId == null) { return(NotFound()); } var lastNumber = 0; var groupRatingControls = _context.RatingControls.Where(x => x.GroupId == groupId && x.SemesterDisciplineId == disciplineId); if (groupRatingControls.Any()) { lastNumber = groupRatingControls.OrderByDescending(x => x.Number).FirstOrDefault().Number; } var newRatingControl = new RatingControl(); newRatingControl.Number = lastNumber + 1; newRatingControl.GroupId = (int)groupId; newRatingControl.SemesterDisciplineId = (int)disciplineId; newRatingControl.CompletionDate = DateTime.Now; _context.Add(newRatingControl); await _context.SaveChangesAsync(); var activities = _context.SemesterDiscipline .Include(x => x.Activities) .ThenInclude(y => y.Type) .Where(x => x.Id == disciplineId) .SelectMany(x => x.Activities) .AsNoTracking() .AsEnumerable(); var pointMultiplier = 1f; if (_context.SemesterDiscipline.Find(disciplineId).ControlType == ControlType.Exam) { var exam = activities.FirstOrDefault(x => x.Type.Name == EXAMTYPENAME); if (exam != null) { activities.ToList().Remove(exam); } pointMultiplier = ((float)(60f / activities.Select(x => x.MaxPoints).Sum())); } else { pointMultiplier = ((float)(100f / activities.Select(x => x.MaxPoints).Sum())); } var students = _context.AcademicGroups .Include(x => x.Students) .ThenInclude(x => x.ActivityProtections .Where(y => activities .Any(z => y.ActivityId == z.Id))) .Where(x => x.Id == groupId) .SelectMany(x => x.Students); var studentsRating = new List <StudentRating>(); foreach (var student in students) { var studentRating = new StudentRating(); studentRating.RatingId = newRatingControl.Id; studentRating.StudentId = student.GradebookNumber; studentRating.Points = (float)(Math.Round(student.ActivityProtections .Select(x => x.Points) .Sum() * pointMultiplier , 1)); studentsRating.Add(studentRating); } _context.AddRange(studentsRating); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Group), new { id = groupId, disciplineId = disciplineId })); }