public virtual ActionResult Details(int questionnaireId) { var results = _resultsRepository.GetResultDetails(questionnaireId); var model = new ResultsListViewModel() { Title = _questionnairesRepository.GetById(questionnaireId).Name, Results = new List <ResultsViewModel>(), Legend = LegendViewModel.Details() }; var questionsIds = results.Select(x => x.QuestionId).Distinct(); foreach (var questionId in questionsIds) { var question = _questionsRepository.GetById(questionId); var questionResults = new ResultsViewModel() { Id = questionId, Text = $"{question.OrderNumber}. {question.Text}", Options = _answersRepository.GetDescriptions(questionId), DictionaryGroups = new List <DictionaryGroupViewModel>() }; var dictionaryGroupNames = results.Select(x => x.DictionaryTypeName).Distinct(); foreach (var dictionaryType in dictionaryGroupNames) { var dictionaryGroup = new DictionaryGroupViewModel() { GroupName = dictionaryType, DictionaryItems = new List <DictionaryItemViewModel>() }; var dictionaryItemsIds = results.Where(x => x.DictionaryTypeName == dictionaryType).Select(x => x.BaseDictionaryId).Distinct(); foreach (var itemId in dictionaryItemsIds) { var dictionaryItem = new DictionaryItemViewModel() { ItemName = _dictionariesRepository.GetById(itemId).Value, Badge = results.First(x => x.BaseDictionaryId == itemId).IntervieweeCount.ToString(), Values = new List <ValueViewModel>() }; var answersIdList = results.Where(x => x.QuestionId == questionId).Select(x => x.AnswerId).Distinct(); foreach (var answerId in answersIdList) { var resultItem = results .Where(x => x.QuestionId == questionId) .Where(x => x.BaseDictionaryId == itemId) .Where(x => x.AnswerId == answerId) .FirstOrDefault(); var value = new ValueViewModel() { Badge = resultItem.AnswersCount.ToString(), Count = resultItem.AnswersCount, TotalCount = resultItem.TotalAnswersCount }; value.SetValueByPercentage(); dictionaryItem.Values.Add(value); } dictionaryGroup.DictionaryItems.Add(dictionaryItem); } questionResults.DictionaryGroups.Add(dictionaryGroup); } model.Results.Add(questionResults); } return(View(MVC.Application.Results.Views.Results, model)); }