获取数据-递归加载
private List<MenuTree> menuTrees = new List<MenuTree>(); public async Task<IEnumerable<MenuTree>> GetMenuTrees() { IEnumerable<MenuTree> tree = await base.GetAllAsync(); menuTrees = tree.ToList(); return GetTree("0", menuTrees); } private List<MenuTree> GetTree(string printId, List<MenuTree> node) { List<MenuTree> mainNodes = node.Where(x => x.ParentMenuId == printId).ToList(); List<MenuTree> otherNodes = node.Where(x => x.ParentMenuId != printId).ToList(); foreach (MenuTree dpt in mainNodes) { dpt.Children = GetTree(dpt.MenuId, otherNodes); } return mainNodes; }
二、数据库表结构
CREATE TABLE [dbo].[Sys_Menu]( [MenuId] [nvarchar](40) NOT NULL, [MenuName] [nvarchar](64) NOT NULL, [ParentMenuId] [nvarchar](40) NULL, [Level] [int] NULL, [Url] [nvarchar](256) NULL, [SourceType] [int] NULL, [Icon] [nvarchar](128) NULL, [OrderIndex] [int] NULL, [Deleted] [int] NOT NULL, [CreatedTime] [datetime] NULL, [CreatedBy] [bigint] NULL, [UpdatedTime] [datetime] NULL, [UpdatedBy] [bigint] NULL, PRIMARY KEY CLUSTERED ( [MenuId] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO