public async Task <ListResult <UserMenuTree> > GetMenusByUserIdAsync(Guid id) { var result = new ListResult <UserMenuTree>(); List <Menu> menus = await _menuRepository.GetMenusByUserIdAsync(id); Guid?[] parentIds = menus.Select(d => d.ParentId).Distinct().ToArray(); var parentMenus = await _menuRepository.GetListAsync(Specification <Menu> .Eval(d => parentIds.Any(x => x == d.Id))); var unionMenus = menus.Union(parentMenus); var treeList = new List <UserMenuTree>(); foreach (var item in unionMenus.Where(d => d.ParentId == null)) { var tree = new UserMenuTree(); tree.Id = item.Id; tree.Title = item.Name; tree.Icon = item.Icon; tree.Href = item.BaseUrl; tree.Spread = true; tree.Children = Recursion(unionMenus.ToList(), item.Id); treeList.Add(tree); } result.data = treeList; return(result); }
private List <UserMenuTree> Recursion(List <Menu> menuList, Guid?id) { var treeList = new List <UserMenuTree>(); var menus = menuList.Where(d => d.ParentId == id); foreach (var menu in menus) { var tree = new UserMenuTree(); tree.Id = menu.Id; tree.Title = menu.Name; tree.Icon = menu.Icon; tree.Href = menu.BaseUrl; tree.Children = Recursion(menuList, menu.Id); treeList.Add(tree); } return(treeList); }