list 递归 转 tree


        public List<TreeJsonM> GetTreeJsonMList(UserAdmin UserAdmin, ObjType objType)
        {
            Expression<Func<Sys_SupplieCate, bool>> where = a => a.Status != (int)Status.Disable
 && a.CustType == (int)objType && a.CompanyId == UserAdmin.CompanyId && a.UnionId == UserAdmin.UnionID;

            List<Sys_SupplieCate> listDep = GetALL(where).OrderBy(a => a.Id).ToList();


            return ConvertTreeJsonMList(listDep);
        }

        //错误的方式
        //List<TreeJsonM> ConvertTreeJsonM(List<Sys_SupplieCate> sys_SupplieCates,int parentId=0)
        //{
        //    List<TreeJsonM> treeJsonMs = new List<TreeJsonM>();
        //    List<Sys_SupplieCate> childs = sys_SupplieCates.Where(a => (a.ParentId==0||a.ParentId==null)
        //    || a.ParentId == parentId).ToList();

        //    if (childs.Count>0)
        //    {
        //        foreach (var item in childs)
        //        {
        //            List<Sys_SupplieCate> aa = sys_SupplieCates;
        //            treeJsonMs.Add(new TreeJsonM() {
        //                id=item.Id,
        //                name = item.Name,
        //                children = ConvertTreeJsonM(aa, item.Id),
        //            });
        //        }
        //    }
        //    return treeJsonMs;
        //}



        List<TreeJsonM> ConvertTreeJsonMList(List<Sys_SupplieCate> sys_SupplieCates, int parentId = 0)
        {
            List<TreeJsonM> treeJsonMs = new List<TreeJsonM>();
            //开始循环根节点
            foreach (var item in sys_SupplieCates.Where(a => a.ParentId == 0 || a.ParentId == null).ToList())
            {
                treeJsonMs.Add(ConvertTreeJsonM(sys_SupplieCates, new TreeJsonM() { id=item.Id,
                name=item.Name}));
            }
            return treeJsonMs;
        }

        //赋值节点的子节点集合
        TreeJsonM ConvertTreeJsonM(List<Sys_SupplieCate> sys_SupplieCates, TreeJsonM treeJsonM)
        {

            List<Sys_SupplieCate> childs = sys_SupplieCates.Where(a =>a.ParentId == treeJsonM.id).ToList();
            if (childs.Count > 0)
            {
                List<TreeJsonM> new_childs = new List<TreeJsonM>();
                foreach (var item in childs)
                {
                    TreeJsonM childrens = ConvertTreeJsonM(sys_SupplieCates, new TreeJsonM()
                    {
                        id = item.Id,
                        name = item.Name
                    });
                    new_childs.Add(childrens);
                }
                treeJsonM.children = new_childs;
            }
            return treeJsonM;
        }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值