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