[英]Transforming data from flat array into hierarchical structure
我正在尋求從平面列表到分層結構的數據轉換。 我怎樣才能以可讀的方式實現這一目標,但性能仍然可以接受,並且我可以利用任何.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 }
]
}
]
}
]
}
“點擊數”是指屬於該集團的公司數量。
為了澄清,我需要將List<Company>
轉換為List<IndustryNode>
NOT序列化List<IndustryNode>
嘗試這個:
private static IEnumerable<Industry> GetAllIndustries(Industry ind)
{
yield return ind;
foreach (var item in ind.ChildIndustries)
{
foreach (var inner in GetAllIndustries(item))
{
yield return inner;
}
}
}
private static IndustryNode[] GetChildIndustries(Industry i)
{
return i.ChildIndustries.Select(ii => new IndustryNode()
{
IndustryName = ii.IndustryName,
Hits = counts[ii],
ChildIndustryNodes = GetChildIndustries(ii)
}).ToArray();
}
private static Dictionary<Industry, int> counts;
static void Main(string[] args)
{
List<Company> companies = new List<Company>();
//...
var allIndustries = companies.SelectMany(c => GetAllIndustries(c.Industry)).ToList();
HashSet<Industry> distinctInd = new HashSet<Industry>(allIndustries);
counts = distinctInd.ToDictionary(e => e, e => allIndustries.Count(i => i == e));
var listTop = distinctInd.Where(i => i.ParentIndustry == null)
.Select(i => new IndustryNode()
{
ChildIndustryNodes = GetChildIndustries(i),
Hits = counts[i],
IndustryName = i.IndustryName
}
);
}
未經測試
嘗試使用json序列化器來實現此目的。 我看到你的數據結構還可以,這只是序列化問題。
var industryNodeInstance = LoadIndustryNodeInstance();
var json = new JavaScriptSerializer().Serialize(industryNodeInstance);
如果您想在序列化器之間進行選擇,請參閱: http : //www.servicestack.net/benchmarks/#burningmonk-benchmarks
LoadIndustryNodeInstance方法
構建List<Industry>
轉換IndustryTree = List<IndustryNode>
實現Tree方法,例如Traverse。 試着看看C#中的Tree數據結構
這里有一些偽造的代碼,可能會讓你一路走來。 我創建一個地圖/字典索引並用公司列表填充它。 然后我們從索引中提取頂級節點。 請注意,可能存在邊緣情況(例如,最初可能需要部分填充此索引,因為您的公司似乎沒有引用任何頂級節點,因此必須以其他方式填充這些節點) 。
Dictionary<String, IndustryNode> index = new Dictionary<String, IndustryNode>();
public void insert(Company company)
{
if(index.ContainsKey(company.Industry.IndustryName))
{
index[company.Industry.IndustryName].hits++;
}
else
{
IndustryNode node = new IndustryNode(IndustryName=company.Industry, Hits=1);
index[node.IndustryName] = node;
if(index.ContainsKey(company.Industry.ParentIndustry.IndustryName))
{
index[company.Industry.ParentIndustry.IndustryName].ChildrenIndustries.Add(node);
}
}
}
List<IndustryNode> topLevelNodes = index
.Where(kvp => kvp.Item.ParentIndustry == null)
.ToList(kvp => kvp.Item);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.