c#后端树形结构递归
作者:互联网
public async Task<List<TreeShow>> GetDepartmentTreeAll()
{
var list = _context.Department.ToList();
List<TreeShow> menus = new List<TreeShow>();
var menu = list.Where(m => m.DeptPId == 1).Select(m => new TreeShow
{
DeptPId = m.DeptPId,
DeptId = m.DeptId,
DeptName = m.DeptName
}).ToList();
GetDepartmentTree(menu);
return menu;
}
private void GetDepartmentTree(List<TreeShow> menus)
{
var list = _context.Department.ToList();
foreach (var item in menus)
{
var _list = list.Where(s => s.DeptPId == item.DeptId).Select(m => new TreeShow
{
DeptPId= item.DeptPId,
DeptId=item.DeptId,
DeptName=item.DeptName
}).ToList();
if (_list!=null)
{
item.MenuData.AddRange(_list);
GetDepartmentTree(_list);
}
}
}
标签:DeptId,递归,c#,list,DeptPId,item,树形,var,DeptName 来源: https://www.cnblogs.com/DuoDongBrain/p/16699623.html