Net Core教程

c#后端树形结构递归

本文主要是介绍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);
}
}
}

这篇关于c#后端树形结构递归的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!