3

フラットリストから階層構造へのデータ変換を検討しています。どうすれば読みやすい方法でこれを達成できますが、パフォーマンスは許容できますか?また、利用できる.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」は、そのグループに分類される企業の数です。

明確にするために、 a を NOT シリアライズ a に変換する必要がList<Company>ありList<IndustryNode> ますList<IndustryNode>

4

4 に答える 4

1

これを試して:

    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
                                }
                        );
    }

テストされていない

于 2013-10-15T15:30:36.037 に答える
0

シリアライザーを探しています。MSFT には VS ネイティブのものがありますが、私は無料の Newtonsofts が好きです。MSFT のドキュメントとサンプルはこちら、Newtonsoft のドキュメントはこちらです。

Newtonsoft は無料で、簡単かつ高速です。

于 2013-10-15T15:31:43.673 に答える
0

この目的のために json シリアライザーを使用してみてください。データ構造に問題がないことがわかりました。これは単なるシリアル化の問題です。

var industryNodeInstance = LoadIndustryNodeInstance();

var json = new JavaScriptSerializer().Serialize(industryNodeInstance);

シリアライザーから選択したい場合は、これを参照してください: http://www.servicestack.net/benchmarks/#burningmonk-benchmarks

LoadIndustryNodeInstance メソッド

  • 建てるList<Industry>

  • 変換IndustryTree = List<IndustryNode>

  • Traverse などの Tree メソッドを実装します。C#で Tree データ構造を見てみる

于 2013-10-15T15:44:34.043 に答える