如何在使用包含多层子集合的对象集合时,通过linq获得与doc.Descendants()类似的功能?
需要获取的数据包含在最后一个嵌套集合中,所有其他父级集合只是分组。 我可以将集合转换为XDocument并调用descendants函数,但我更喜欢针对此对象集合模拟该功能。
public class ProductLine
{
public string Id {get;set;}
public string ParentId {get;set;}
public string Name {get;set;}
public string Type {get;set;}
public string Level {get;set;}
public IEnumerable<ProductLine> Children {get;set;}
}
我可以有一个包含子产品线列表的产品线列表。嵌套级别可以根据数据设置的方式而变化,因此我永远不知道有多少级别。最底层的列表将具有Type =“Model”,而之前的每个列表将具有Type =“Series”,从而产生以下内容:
Series1
Series2
Series3
Model1
Model1
Series2
Model3
Model4