话不多说,直接show me the code ,no bb
数据库表结构如下:
Organization表:
Id 主键
Name 名称
ParentId 父Id
数据库的数据如下:
很明显,这是一个多层级的结构,我们需要把他组装成一个树形结构,返回给前端:
定义一个treeModel类:
public class treemodel
{
public organization parent { get; set; }
public List<treemodel> childrenList { get; set; } = new List<treemodel>();
}
写一个获取子结构体的方法:GetChildrentList
private List<treemodel> GetChildrentList(int parentId)
{
var query = _mydbContext.organizations.Where(e => e.ParentId == parentId).ToList();
//return query.ToList().Concat(query.ToList().SelectMany(t =&g