public static CategoryHierarchyDto Compose(List<DetailedSubCategoryDto> data) { var result = new CategoryHierarchyDto(); var categoryIds = data.Select(s => s.CategoryId).Distinct().ToList(); categoryIds.ForEach(id => { var category = data.First(f => f.CategoryId == id); var hierarchy = new Hierarchy { Id = category.CategoryId, Text = category.CategoryName }; hierarchy.AddAttribute("Description", category.Description); var subCategories = data.Where(f => f.CategoryId == id).ToList(); subCategories.ForEach(s => { var subHierarchy = new Hierarchy { Id = s.SubCategoryId, Text = s.SubCategoryName }; hierarchy.Items.Add(subHierarchy); }); result.Items.Add(hierarchy); }); return result; }
public static IEnumerable <HomePageCategoryViewModel> FromHierachicalTemplate(CategoryHierarchyDto data) { foreach (var category in data.Items) { var vm = new HomePageCategoryViewModel { Id = category.Id, Name = category.Text, Description = category.Attributes["Description"] }; if (category.Items.Any()) { vm.SubItems = category.Items.Select(s => new SubCategoryViewModel { Id = s.Id, Name = s.Text }).ToList(); } yield return(vm); } }