首页 > 解决方案 > 订购嵌套内部集合的 C# 最佳实践方法

问题描述

我正在以更好的方式寻找我的收藏品的订单。我有 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; }
}

标签: c#performancelinqlambda

解决方案


你可以用你SortingSelectTokens方法来实现JToken

1)将您的 json 解析为JToken.

2)Es在整个JToken.

3) 选择每个 json 对象作为JObject.

Jobject4) 并用 对所有s 进行排序OrderBy

5)最后打印你的结果。

class Program
{
    static void Main(string[] args)
    {
        string str = File.ReadAllText(@"Path to your json file");

        //Step 1
        JToken jToken = JToken.Parse(str);

        //Step 2 to 4
        var result = jToken.SelectTokens("..Es").Select(x => x.Select(y => (JObject)y).OrderBy(z => z["Total"])).ToList();

        //Step 5
        foreach (var i in result)
        {
            foreach (var j in i)
                Console.WriteLine($"Type: {j["Type"]} \t Total: {j["Total"]}");
            Console.WriteLine();
        }

        Console.ReadLine();
    }
}

输出:

在此处输入图像描述


推荐阅读