首页 > Web开发 > 详细

.Net递归加载菜单树

时间:2021-06-23 09:34:24      阅读:29      评论:0      收藏:0      [点我收藏+]

获取数据-递归加载

 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

 

 

 

技术分享图片

 

 

技术分享图片

 

.Net递归加载菜单树

原文:https://www.cnblogs.com/ABC-wangyuhan/p/14920900.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!