其他分享
首页 > 其他分享> > .Net 取树形结构的数据

.Net 取树形结构的数据

作者:互联网

最近遇到了无限层级数据要读取的问题,所有就写了个。

根据当前所有父级,查询出子级内容

 

private void GetTypeOfWorkforTree(out List<TypeOfWorkDto> workParent, out List<TypeOfWorkDto[]> workList)
        {
            var workData = _typeOfWorkAppService.GetAllTypeOfWorkForTree().Result;//数据库的数据
            workParent = workData.Where(p => p.Parent == null).ToList();

            workList = new List<TypeOfWorkDto[]>();
            for (var item=0;item<workParent.Count();item++)//工头
            {
                var count = new List<TypeOfWorkDto>();
                count.Add(workParent[item]);
                for (var i = 0; i < count.Count(); i++)//工头下面的种类
                {
            //查询子级
                    var resultCount = workData.Where(x => x.ParentId == count[i].Id).ToList();
                    foreach (var u in resultCount)
                    {
                            count.Add(u);
                    }
                }
                workList.Add(count.ToArray());//合并类别
            }
        }

 

如果不取父级

 private void GetTypeOfWorkforTree(out List<TypeOfWorkDto[]> workList)
        {
            var workData = _typeOfWorkAppService.GetAllTypeOfWorkForTree().Result;

            workList = new List<TypeOfWorkDto[]>();
            for (var item=0;item<workData.Count();item++)//所有,取一条
            {
          foreach(var Ifin in workList)
          {
            Ifin.Containes(item);
            continue;//当前的子父级已存在,跳出
          }
                var count = new List<TypeOfWorkDto>();
                count.Add(workData[item]);
                for (var i = 0; i < count.Count(); i++)//取当前有关联
                {
            //查询父级 子级
                    var resultCount = workData.Where(x => x.ParentId == count[i].Id || x.Id == count[i].ParentId).ToList();
                    foreach (var u in resultCount)
                    {
                        if (!count.Contains(u))//会有重复的,排除掉
                            count.Add(u);
                    }
                }
                workList.Add(count.ToArray());//合并类别
            }
        }

 

标签:count,workData,item,Add,树形,var,Net,workList,结构
来源: https://www.cnblogs.com/Uyde/p/11160367.html