目录
一、需求:
给小明一家生成族谱树
二、实现代码
cs
using Newtonsoft.Json;
class Person
{
public int Id { get; set; }
public int? ParentID { get; set; }
public string Name { get; set; }
}
class TreeDto : Person
{
[JsonProperty(Order =99)]
public List<TreeDto> Children = new List<TreeDto>();
}
class Program
{
static List<Person> PersonList = new List<Person>()
{
new Person(){ Id=1,ParentID=null,Name="小明曾祖父",},
new Person(){ Id=2,ParentID=1,Name="小明爷爷",},
new Person(){ Id=3,ParentID=1,Name="小明二爷爷",},
new Person(){ Id=4,ParentID=1,Name="小明三爷爷",},
new Person(){ Id=5,ParentID=2,Name="小明爸爸",},
new Person(){ Id=6,ParentID=2,Name="小明叔叔",},
new Person(){ Id=7,ParentID=5,Name="小明本人",},
new Person(){ Id=8,ParentID=5,Name="小明妹妹",},
new Person(){ Id=9,ParentID=7,Name="小明儿子",}
};
static void Main()
{
TreeDto tree = BuildTree(null);//传入列表根节点【小明曾祖父】,开始递归
var result = JsonConvert.SerializeObject(tree);//实体=>JSON打印
Console.WriteLine(result);
}
static TreeDto BuildTree(int? parentId)
{
var children = PersonList
.Where(p => p.ParentID == parentId)
.Select(p => new TreeDto
{
Id = p.Id,
ParentID = p.ParentID,
Name = p.Name,
Children = BuildTree(p.Id).Children
})
.ToList();
return new TreeDto
{
Id = parentId ?? 0, //父级Id
ParentID = null,
Name = "族谱",
Children = children
};
}
}
【效果】
javascript
{
"Id": 0,
"ParentID": null,
"Name": "族谱",
"Children": [
{
"Id": 1,
"ParentID": null,
"Name": "小明曾祖父",
"Children": [
{
"Id": 2,
"ParentID": 1,
"Name": "小明爷爷",
"Children": [
{
"Id": 5,
"ParentID": 2,
"Name": "小明爸爸",
"Children": [
{
"Id": 7,
"ParentID": 5,
"Name": "小明本人",
"Children": [
{
"Id": 9,
"ParentID": 7,
"Name": "小明儿子",
"Children": []
}
]
},
{
"Id": 8,
"ParentID": 5,
"Name": "小明妹妹",
"Children": []
}
]
},
{
"Id": 6,
"ParentID": 2,
"Name": "小明叔叔",
"Children": []
}
]
},
{
"Id": 3,
"ParentID": 1,
"Name": "小明二爷爷",
"Children": []
},
{
"Id": 4,
"ParentID": 1,
"Name": "小明三爷爷",
"Children": []
}
]
}
]
}
三、递归代码
cs
static TreeDto BuildTree(int? parentId)
{
var children = PersonList
.Where(p => p.ParentID == parentId)
.Select(p => new TreeDto
{
Id = p.Id,
ParentID = p.ParentID,
Name = p.Name,
Children = BuildTree(p.Id).Children
})
.ToList();
return new TreeDto
{
Id = parentId ?? 0, //父级Id
ParentID = null,
Name = "族谱",
Children = children
};
}
四、思考
1.设定序列化的字段顺序,例如将Children这个字段放最后面
2.递归的写法注释
3.如果根节点不想设为族谱,如何获取从曾祖父开始的递归树
将
cs
var result = JsonConvert.SerializeObject(tree);//族谱开始
改为
cs
var result = JsonConvert.SerializeObject(tree.Children.FirstOrDefault());//曾祖父开始
4.如果想在族谱上加一个根节点,如何操作
【代码】
cs
TreeDto tree = BuildTree(null);//获取递归结果
var result = new TreeDto
{
Id = 1000,
Name = "新增根节点",
Children = new List<TreeDto>() { tree }
};
【效果】