订购嵌套内部集合的C#最佳实践方法

时间:2018-10-27 09:50:17

标签: c# performance linq lambda

我正在寻找一种更好的订购方式。我有下面的json对象,

   [{
    "Type": "A",
    "Bs": [{
        "Type": "B",
        "Cs": [{
            "Type": "C",
            "Ds": [{
                "Type": "D",
                "Es": [{
                    "Type": "E",
                    "Total": 10
                },
                {
                    "Type": "E",
                    "Total": 20
                },
                {
                    "Type": "E",
                    "Total": 1
                }]
            },
            {
                "Type": "D",
                "Es": [{
                    "Type": "E",
                    "Total": 100
                },
                {
                    "Type": "E",
                    "Total": 50
                },
                {
                    "Type": "E",
                    "Total": 10
                }]
            }]
        }]
    },
    {
        "Type": "B",
        "Cs": null
    }]
}]

我想以更好的方式按内部集合( E模型)订购。我已经通过 SortByTotal 方法实现了我想要的,但是我想对其进行改进。这是我的解决方法。

private static void SortByTotal(List<A> list)
{
    foreach (var a in list)
    {
        if (a.Bs == null) continue;
        foreach (var b in a.Bs)
        {
            if (b.Cs == null) continue;
            foreach (var c in b.Cs)
            {
                if (c.Ds == null) continue;
                foreach (var d in c.Ds)
                {
                    if (d.Es == null) continue;
                    d.Es = d.Es.OrderBy(x => x.Total).ToList();
                }
            }
        }
    }
}

这是我所有带有模型类和示例对象的代码

注释:基本上需要改进 SortByTotal 方法

class Program
{
    static void Main(string[] args)
    {
        var list = new List<A>
                { new A { Bs = new List<B> { new B { Cs = new List<C> { new C { Ds = new List<D> {
                    new D {
                        Es = new List<E> { new E {Total = 10}, new E {Total = 20}, new E {Total = 1} }
                    },
                    new D {
                        Es = new List<E> { new E {Total = 100}, new E {Total = 50}, new E {Total = 10} }
                    }
                } } } }, new B() } } };

        Console.WriteLine("before sort");
        var beforeSortList = list;

        SortByTotal(list);

        Console.WriteLine("after sort");
        var afterSortList = list;


        Console.ReadKey();
    }

    private static void SortByTotal(List<A> list)
    {
        foreach (var a in list)
        {
            if (a.Bs == null) continue;
            foreach (var b in a.Bs)
            {
                if (b.Cs == null) continue;
                foreach (var c in b.Cs)
                {
                    if (c.Ds == null) continue;
                    foreach (var d in c.Ds)
                    {
                        if (d.Es == null) continue;
                        d.Es = d.Es.OrderBy(x => x.Total).ToList();
                    }
                }
            }
        }
    }
}

class A
{
    public string Type { get; set; } = "A";

    public List<B> Bs { get; set; }
}

class B
{


    public string Type { get; set; } = "B";

    public List<C> Cs { get; set; }
}

class C
{
    public string Type { get; set; } = "C";

    public List<D> Ds { get; set; }
}

class D
{
    public string Type { get; set; } = "D";

    public List<E> Es { get; set; }
}

class E
{
    public string Type { get; set; } = "E";

    public int Total { get; set; }
}

1 个答案:

答案 0 :(得分:1)

您可以使用Sorting的{​​{1}}方法来实现SelectTokens

1)将您的json解析为JToken

2)在整个JToken中获取所有密钥为Es的令牌。

3)选择每个json对象作为JToken

4)并用JObject对所有Jobject进行排序。

5)最后打印结果。

OrderBy

输出:

enter image description here