public static RCategory[] RemoveCurrentTree(RCategory[] categories, RCategory category) { Dictionary <string, IList <RCategory> > dictionary = BuildTree(categories); IList <RCategory> categoriesRemove = new List <RCategory>(); if (dictionary.ContainsKey(category.Id)) { var childs = dictionary[category.Id]; foreach (var child in childs) { //dictionary[menu.Id].Remove(child); categoriesRemove.Add(child); RemoveChild(dictionary, child, ref categoriesRemove); } } if (dictionary.ContainsKey(category.ParentId)) { categoriesRemove.Add(category); RemoveChild(dictionary, category, ref categoriesRemove); } var rootItem = dictionary["#"].FirstOrDefault(p => p.Id == category.Id); if (rootItem != null) { dictionary["#"].Remove(rootItem); } if (categoriesRemove.Count > 0) { foreach (var rCategory in categoriesRemove) { dictionary[rCategory.ParentId].Remove(rCategory); } } return(dictionary.Values.SelectMany(p => p).ToArray()); }
private static void RemoveChild(Dictionary <string, IList <RCategory> > dictionary, RCategory category, ref IList <RCategory> categoriesRemove) { if (dictionary.ContainsKey(category.Id)) { var childs = dictionary[category.Id]; foreach (var child in childs) { //dictionary[menu.Id].Remove(child); categoriesRemove.Add(child); RemoveChild(dictionary, child, ref categoriesRemove); } } }