public async Task<List<DepartView2>> GetDepartmentTreeAsync()
{
var departmentViews = await auap_Field_CommentDAL.GetDepartmentViewsAllAsync();
foreach (var item in departmentViews)
{
item.child = departmentViews.Where(x => x.ParentId == item.DepartId).ToList();
item.IsHaveChild = item.child.Count > 0 ? 1 : 0;
}
List<DepartView2> tree = new List<DepartView2>();
int rootId = 0;
var list_01 = departmentViews.Where(x => x.DepartId == rootId).ToList();
Action<DepartView2> func = null;
func = (q) =>
{
var find = departmentViews.FirstOrDefault(x => x.DepartId == q.id);
if (find.child.Count() > 0)
{
List<DepartView2> childList = new List<DepartView2>();
foreach (var item in find.child)
{
DepartView2 departView3 = new DepartView2();
departView3.id = item.DepartId;
departView3.title = item.DepartName;
childList.Add(departView3);
func(departView3);
}
q.children = childList;
}
};
foreach (var item in list_01)
{
DepartView2 departView3 = new DepartView2();
departView3.id = item.DepartId;
departView3.title = item.DepartName;
func(departView3);
tree.Add(departView3);
}
return tree;
}
public class DepartView2
{
public int id { get; set; }
public string title { get; set; }
public List<DepartView2> children { get; set; }
}
public class DepartmentView
{
public int DepartId { get; set; }
public string DepartName { get; set; }
public int? ParentId { get; set; }
public List<DepartmentView> child { get; set; } = new List<DepartmentView>();
public int IsHaveChild { get; set; }
public int Level { get; set; }
}
|