最近遇到了无限层级数据要读取的问题,所有就写了个。
根据当前所有父级,查询出子级内容
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());//合并类别 } }