我有一个数据表,其中包含有关目录树文件夹和文档的信息 该表包含列ID和列parentId
+----+--------+-----------+---------+
| id | parent | is_folder | details |
+----+--------+-----------+---------+
| 1 | | TRUE | … |
| 2 | 1 | TRUE | … |
| 3 | 1 | TRUE | … |
| 4 | 1 | FALSE | … |
| 5 | 2 | TRUE | … |
| 6 | 5 | FALSE | … |
| 7 | 5 | TRUE | … |
| 8 | 7 | FALSE | … |
| 9 | 3 | FALSE | … |
| 10 | 3 | FALSE | … |
+----+--------+-----------+---------+
我想将此列表构建为递归目录树
返回IEnumerable<item>
public class item
{
public bool isFolder { get; set; }
public int id { get; set; }
public int parentId { get; set; } //should???
public string details { get; set; }
public IEnumerable<item> children { get; set; }
}
答案 0 :(得分:0)
为每行创建一个项目,就像通常那样。然后,做这样的事情来创建孩子们:
foreach(var item in items)
item.children = items.Where(x => x.parentId == item.id);