C#匿名委托递归,部门树形
Posted 棉晗榜
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#匿名委托递归,部门树形相关的知识,希望对你有一定的参考价值。
/// <summary>
/// 获取AUAP数据库部门树形
/// </summary>
/// <returns></returns>
/// 创建时间:2022-3-14 09:26:29;
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;
//int.TryParse(MyConfigReader.GetConfigValue("root_dept_id"),out rootId);
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前端数据格式 模型
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;
/// <summary>
/// 前端框架,部门树形数据格式
/// </summary>
public class DepartView2
public int id get; set;
public string title get; set;
public List<DepartView2> children get; set;
/// <summary>
/// 部门树形
/// </summary>
public class DepartmentView
/// <summary>
/// 部门id
/// </summary>
public int DepartId get; set;
/// <summary>
/// 部门名称
/// </summary>
public string DepartName get; set;
/// <summary>
/// 父级id
/// </summary>
public int? ParentId get; set;
/// <summary>
/// 子集
/// </summary>
public List<DepartmentView> child get; set; = new List<DepartmentView>();
/// <summary>
/// 是否有子集,1=有,0=没有
/// </summary>
public int IsHaveChild get; set;
/// <summary>
/// 层级
/// </summary>
public int Level get; set;
以上是关于C#匿名委托递归,部门树形的主要内容,如果未能解决你的问题,请参考以下文章