public async Task <IActionResult> ScanSystem() { var studentClassGrades = await _schoolService.GetStudentClassGradesAysnc(); var teachers = await _schoolService.GetTeachersAsync(); var parents = await _schoolService.GetParentsAsync(); var problemStudents = new List <Student>(); var parentsWithProblemStudents = new List <Parent>(); var teachersWithProblemStudents = new List <Teacher>(); foreach (var studentClassGrade in studentClassGrades) { if (studentClassGrade.Grade <= 50) { problemStudents.Add(studentClassGrade.Student); if (!teachersWithProblemStudents.Exists(t => t.ClassId == studentClassGrade.ClassId)) { teachersWithProblemStudents.Add(teachers.Find(t => t.ClassId == studentClassGrade.ClassId)); } if (!parentsWithProblemStudents.Exists(p => p.StudentId == studentClassGrade.StudentId)) { parentsWithProblemStudents.Add(parents.Find(p => p.StudentId == studentClassGrade.StudentId)); } } else if (studentClassGrade.Grade <= 70) { if (studentClassGrade.Student.Profile.FavoriteSubject == studentClassGrade.Class.Subject) { problemStudents.Add(studentClassGrade.Student); if (!teachersWithProblemStudents.Exists(t => t.ClassId == studentClassGrade.ClassId)) { teachersWithProblemStudents.Add(teachers.Find(t => t.ClassId == studentClassGrade.ClassId)); } if (!parentsWithProblemStudents.Exists(p => p.StudentId == studentClassGrade.StudentId)) { parentsWithProblemStudents.Add(parents.Find(p => p.StudentId == studentClassGrade.StudentId)); } } } } var recipients = new List <IEmail>(); recipients.AddRange(parentsWithProblemStudents); recipients.AddRange(teachersWithProblemStudents); await _emailService.EmailAlertAsync(recipients); return(View(nameof(Index))); }