c#层次列表(树)到扁平列表
我有以下课程:c#层次列表(树)到扁平列表,c#,C#,我有以下课程: public class Item { public Item() { this.Items = new List<Item>(); } public string Id { get; set; } public string ParentId { get; set; } public string Name { get; set; } public DateTime Created { ge
public class Item
{
public Item()
{
this.Items = new List<Item>();
}
public string Id { get; set; }
public string ParentId { get; set; }
public string Name { get; set; }
public DateTime Created { get; set; }
public string Content { get; set; }
public string UserId { get; set; }
List<Item> Children { get; set; }
}
公共类项目
{
公共项目()
{
this.Items=新列表();
}
公共字符串Id{get;set;}
公共字符串ParentId{get;set;}
公共字符串名称{get;set;}
已创建公共日期时间{get;set;}
公共字符串内容{get;set;}
公共字符串用户标识{get;set;}
列出子项{get;set;}
}
这个类将提供一个具有N个深层次(父-子)的树层次结构
因此,基本上,当我将层次结构作为列表时:
List<Item> items = JsonConvert.DeserializeObject<List<Item>>("json in here")
List items=JsonConvert.DeserializeObject(“此处为json”)
结果与此类似:深度可以是X级。可以有一个或多个父级(ParentId=null为顶级)
- 父项(Id:1,ParentId:null)
- 子(Id:1.1,父Id:1)
- 孩子(Id:1.1.1,家长Id:1.1)
- 子(Id:1.2,父Id:1)
- 孩子(Id:1.2.1,家长Id:1.2)
- 孩子(Id:1.2.2,家长Id:1.2)
- 子女(Id:1.3,父母Id:1)
- 子(Id:1.1,父Id:1)
- 父项(Id:2,父项Id:null)
- 孩子(Id:2.1,家长Id:2)
- 子女(Id:2.1.1,父母Id:2.1)
- 孩子(Id:2.2,家长Id:2)
- 子女(Id:2.2.1,父母Id:2.2)
- 子女(Id:2.2.2,父母Id:2.2)
- 子女(Id:2.3,父母Id:2)
- 孩子(Id:2.1,家长Id:2)
- 父项(Id:3,父项Id:null)
列表
编辑:
Id
和ParentId
是guid。您可以向项
类添加如下函数:
public IEnumerable<Item> GetItems()
{
var result = Children.SelectMany(i => i.GetItems()).ToList();
result.Add(this);
return result;
}
如果您公开了儿童,因此它不是私人的,您可以这样做:
public IEnumerable<Item> GetItems(IEnumerable<Item> items)
{
foreach(var item in items)
{
yield return item;
foreach(var child in GetItems(item.Children))
{
yield return child;
}
}
}
public IEnumerable GetItems(IEnumerable items)
{
foreach(项目中的var项目)
{
收益回报项目;
foreach(GetItems(item.Children)中的var子级)
{
退换子女;
}
}
}
然后输入你的父母枚举,得到一个简单的列表。这里有几个选择:
public IEnumerable<Item> GetItems()
{
return
new [] { this }
.Concat(this.Children.SelectMany(i => i.GetItems()));
}
public IEnumerable<Item> GetItems()
{
yield return this;
foreach (var descendent in this.Children.SelectMany(i => i.GetItems()))
{
yield return descendent;
}
}
public IEnumerable GetItems()
{
返回
新[]{this}
.Concat(this.Children.SelectMany(i=>i.GetItems());
}
公共IEnumerable GetItems()
{
收益回报这一点;
foreach(this.Children.SelectMany(i=>i.GetItems())中的var子代)
{
产量-回报后代;
}
}
此递归函数可能有助于将数据保存在平面结构中:
class Test
{
List<Item> Items = new List<Item>();
List<Item> GetList()
{
return getList_Rec(this.Items);
}
private List<Item> getList_Rec(List<Item> items)
{
var result = new List<Item>();
result.AddRange(items);
items.ForEach(x => result.AddRange(x.Children));
return result;
}
}
类测试
{
列表项=新列表();
List GetList()
{
返回getList_Rec(this.Items);
}
私有列表getList_Rec(列表项)
{
var result=新列表();
结果.添加范围(项目);
items.ForEach(x=>result.AddRange(x.Children));
返回结果;
}
}
编写递归函数遍历树。Internet上充满了示例…所以基本上我可以递归地遍历树并将项目添加到一个新列表中,对吗?如果(…)部分是这个答案的话,您可以完全删除它的,效果也一样。@Enigmativity感谢您努力改进答案。
class Test
{
List<Item> Items = new List<Item>();
List<Item> GetList()
{
return getList_Rec(this.Items);
}
private List<Item> getList_Rec(List<Item> items)
{
var result = new List<Item>();
result.AddRange(items);
items.ForEach(x => result.AddRange(x.Children));
return result;
}
}