我正在寻找从平面列表到层次结构的数据转换。我怎样才能以一种可读的方式实现这一点,但在性能上仍然可以接受,是否有任何我可以利用的 .NET 库。我认为这在某些术语中被认为是“方面”(在这种情况下是行业)。
public class Company
{
public int CompanyId { get; set; }
public string CompanyName { get; set; }
public Industry Industry { get; set; }
}
public class Industry
{
public int IndustryId { get; set; }
public string IndustryName { get; set; }
public int? ParentIndustryId { get; set; }
public Industry ParentIndustry { get; set; }
public ICollection<Industry> ChildIndustries { get; set; }
}
现在假设我有一个List<Company>
并且我正在寻找将它转换为List<IndustryNode>
//Hierarchical data structure
public class IndustryNode
{
public string IndustryName{ get; set; }
public double Hits { get; set; }
public IndustryNode[] ChildIndustryNodes{ get; set; }
}
这样生成的对象在序列化后应该如下所示:
{
IndustryName: "Industry",
ChildIndustryNodes: [
{
IndustryName: "Energy",
ChildIndustryNodes: [
{
IndustryName: "Energy Equipment & Services",
ChildIndustryNodes: [
{ IndustryName: "Oil & Gas Drilling", Hits: 8 },
{ IndustryName: "Oil & Gas Equipment & Services", Hits: 4 }
]
},
{
IndustryName: "Oil & Gas",
ChildIndustryNodes: [
{ IndustryName: "Integrated Oil & Gas", Hits: 13 },
{ IndustryName: "Oil & Gas Exploration & Production", Hits: 5 },
{ IndustryName: "Oil & Gas Refining & Marketing & Transporation", Hits: 22 }
]
}
]
},
{
IndustryName: "Materials",
ChildIndustryNodes: [
{
IndustryName: "Chemicals",
ChildIndustryNodes: [
{ IndustryName: "Commodity Chemicals", Hits: 24 },
{ IndustryName: "Diversified Chemicals", Hits: 66 },
{ IndustryName: "Fertilizers & Agricultural Chemicals", Hits: 22 },
{ IndustryName: "Industrial Gases", Hits: 11 },
{ IndustryName: "Specialty Chemicals", Hits: 43 }
]
}
]
}
]
}
其中“Hits”是属于该组的公司数量。
为了澄清,我需要将 aList<Company>
转换为List<IndustryNode>
NOT 序列化 aList<IndustryNode>