private Dictionary <ClassificationDTO, List <UnitSelectionDTO> > AppendIfNotExist( Dictionary <ClassificationDTO, List <UnitSelectionDTO> > dictionary, ClassificationDTO key, UnitSelectionDTO unitSelectionDto) { if (dictionary.ContainsKey(key)) { dictionary[key].Add(unitSelectionDto); } else { dictionary.Add(key, new List <UnitSelectionDTO>() { unitSelectionDto }); } return(dictionary); }
private UnitSelectionDTO FlatToHierarchy(List <DAL.Entities.Tables.UnitTree> list, List <int> expandableClassifications = null, int?topLevelId = null) { bool limited = expandableClassifications == null; foreach (var unitTree in list.Where(x => x.ParentId == topLevelId)) { unitTree.ParentId = -1; } var lookup = new Dictionary <int, UnitSelectionDTO>() { { -1, new UnitSelectionDTO() } }; foreach (var item in list) { UnitSelectionDTO unitSelectionDto = _mapper.Map <UnitSelectionDTO>(item); lookup.Add(item.Id, unitSelectionDto); } foreach (DAL.Entities.Tables.UnitTree item in list) { ClassificationDTO classification = _mapper.Map <ClassificationDTO>(item.UnitClassification); var parentGroup = lookup[item.ParentId.Value].Children; if (limited || expandableClassifications.Contains(item.UnitClassificationId)) { if (lookup.ContainsKey(item.ParentId.Value)) { AppendIfNotExist(parentGroup, classification, lookup[item.Id]); } } else if (!parentGroup.ContainsKey(classification)) { parentGroup.Add(classification, new List <UnitSelectionDTO>()); } } foreach (var unitTree in lookup.Where(x => x.Value.ParentId == -1)) { unitTree.Value.ParentId = topLevelId; } return(lookup[-1]); }