一开始使用下面这个博文里的方式,但每次都要取出全部,后投影新类型,然后用字典组合树。
https://blog.csdn.net/u010162297/article/details/53019101
后面改用下面方式,直接递归投影新类型,代码量少,不用组合。
新类型:
public class DeTModel
{
public string id { set; get; }
public string label { set; get; }
public int parent { set; get; }
public List<DeTModel> children { set; get; }
}
操作方式:
private DeTModel Conv(Sys_department x)
{
var d = new DeTModel
{
id = x.Id.ToString(),
label = x.Department,
parent = x.ParentId.Value,
children = x.Child_Department.Select(c => Conv(c)).ToList()
};
return d;
}
使用方法:
var alldata = //自己的数据
var result = alldata.Select(x => Conv(x));