public IActionResult GetAnswersStatistics() { IEnumerable <GetAnswerDto> answers = _answerRepository.GetAnswers(); List <StatisticsDto> statistics = new List <StatisticsDto>(); var groupedAnswers = answers.OrderBy(ans => ans.QuestionId).GroupBy(ans => ans.QuestionId).Where(group => group.Key != (int)Enum_QuestionType.Opened).ToArray(); foreach (IGrouping <int, GetAnswerDto> questionAnswer in groupedAnswers) { var groupedReplies = questionAnswer.OrderBy(reply => reply.ReplyContent).GroupBy(reply => reply.ReplyContent).ToArray(); Dictionary <string, int> answersInstances = new Dictionary <string, int>(); foreach (IGrouping <string, GetAnswerDto> reply in groupedReplies) { answersInstances.Add(reply.Key, reply.ToArray().Length); } statistics.Add(new StatisticsDto() { QuestionContent = _questionRepository.GetQuestion(questionAnswer.Key).Content, AnswerStatistics = answersInstances }); } return(Json(statistics)); }