public ActionResult NormalizeParticipantsWithDuplicatedParticipantScores() { var result = new StringBuilder("<p>Done! Deleted Participant scores:</p><ol>"); var problemIds = this.Data.Problems.AllWithDeleted().Select(pr => pr.Id).ToList(); foreach (var problemId in problemIds) { var participantScoresRepository = new EfGenericRepository <ParticipantScore>(new OjsDbContext()); var scoresMarkedForDeletion = new List <ParticipantScore>(); participantScoresRepository .All() .Where(ps => ps.ProblemId == problemId) .GroupBy(p => new { p.ProblemId, p.ParticipantId }) .Where(participantScoreGroup => participantScoreGroup.Count() > 1) .ForEach(participantScoreGroup => { participantScoreGroup .OrderByDescending(ps => ps.Points) .ThenByDescending(ps => ps.Id) .Skip(1) .ForEach(ps => scoresMarkedForDeletion.Add(ps)); }); if (scoresMarkedForDeletion.Any()) { foreach (var participantScoreForDeletion in scoresMarkedForDeletion) { participantScoresRepository.Delete(participantScoreForDeletion); result.Append($@"<li>ParticipantScore with ParticipantId: {participantScoreForDeletion.ParticipantId} and ProblemId: {participantScoreForDeletion.ProblemId}</li>"); } participantScoresRepository.SaveChanges(); } } result.Append("</ol>"); return(this.Content(result.ToString())); }