欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

EF数据库迁移命令 C#无限极递归树及三元运算符简写

程序员文章站 2022-05-31 09:19:13
...

1、EF数据库迁移控制台命令:

Enable-Migrations
Add-Migration init
update-database

2、C#三元运算符简写

is_seckill_activity ?is_seckill_activity  :false
is_seckill_activity ?? false

3、C#无限极递归树

public JsonResult GetTreeMenuList()
        {
            var menuTreeList = authService.GetTreeMenuList(-1);
            var treeList = new List<TreeListViewModel>();
            foreach (var item in menuTreeList)
            {
                var temp = new TreeListViewModel()
                {
                    id = item.id,
                    text = item.name,
                    pid = item.pid
                };
                treeList.Add(temp);
            }
            var treeItem = new TreeListViewModel()
            {
                id = 0,
                text = "跟节点",
                nodes = new List<TreeListViewModel>()
            };
            var result = BuildTreeList(treeList, treeItem);
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        private TreeListViewModel BuildTreeList(List<TreeListViewModel> all, TreeListViewModel curItem)
        {
            var subItems = all.Where(m => m.pid == curItem.id).ToList();
            curItem.nodes = new List<TreeListViewModel>();
            curItem.nodes.AddRange(subItems);
            foreach (var subItem in subItems)
            {
                BuildTreeList(all, subItem);
            }
            return curItem;
        }