public ActionResult SearchAll(string searchTerm, string indicatorId) { var model = new List<DomainIndicatorsSearchResult>(); // Get a distinct list of records that match the search var search = new IndicatorSearch(); IEnumerable<IndicatorMetadataTextValue> indicatorMatch = string.IsNullOrEmpty(indicatorId) ? search.SearchByText(searchTerm) : search.SearchByIndicatorId(Convert.ToInt32(indicatorId)); var allAreaTypes = CommonUtilities.GetAllAreaTypes(); var profiles = _profileRepository.GetProfiles(); foreach (var indicatorMetadataTextValue in indicatorMatch) { var groupings = CommonUtilities .GetGroupingsByIndicatorIds(new List<int> { indicatorMetadataTextValue.IndicatorId }) .Distinct(new DistinctGroupComparer()) .ToList(); foreach (var grouping in groupings) { Debug.WriteLine(grouping.GroupId); var domainIndicators = new DomainIndicatorsSearchResult(); domainIndicators.GroupId = grouping.GroupId; // Assign grouping metadata var groupingMetadata = CommonUtilities.GetGroupingMetadata(new List<int> { grouping.GroupId }).First(); domainIndicators.GroupName = groupingMetadata.GroupName; domainIndicators.SequenceId = groupingMetadata.Sequence; // Assign profile information var profile = profiles.FirstOrDefault(x => x.Id == groupingMetadata.ProfileId); if (profile != null) { domainIndicators.ProfileName = profile.Name; domainIndicators.UrlKey = profile.UrlKey; // Assign the rest GroupingPlusName groupingPlusNames = _profileRepository .GetGroupingPlusNames(grouping.IndicatorId, grouping.GroupId, grouping.AreaTypeId, profile.Id) .First(x => x.SexId == grouping.SexId && x.AgeId == grouping.AgeId && x.YearRange == grouping.YearRange); groupingPlusNames.AreaType = allAreaTypes.First(x => x.Id == grouping.AreaTypeId).ShortName; groupingPlusNames.ComparatorMethodId = grouping.ComparatorMethodId; groupingPlusNames.ComparatorId = grouping.ComparatorId; domainIndicators.Indicators.Add(groupingPlusNames); model.Add(domainIndicators); } } } model = model.Where(m => m.Indicators.Any()).ToList(); LoadProfileAndDomainDropdowns(); return View("SearchAll", model); }