简体   繁体   中英

C# Sum of Values in a nested dictionary where value is a class Dictionary<string, Dictionary<Int16, CommonData>>

I have a dictionary with following format, where Value is a nested dictionary in which value is a class. I have tried various ways to flatten it, but no hope. I went through most of the related SO questions also. But couldn't find the answer.

Dictionary<string, Dictionary<Int16, CommonData>>

Common Data is a class.

    public class CommonData
    {
        public int Submitted { get; set; }

        public int Delivered { get; set; }

        public int Failed { get; set; }
    }

Now What I need is I need to sort the main dictionary based on the Sum of the data in the CommonData Class. I need to sum up Submitted + Delivered + Failed in the Values and sort the main dictionary according to this summed value.

How can we achieve this using LINQ. Could you help me with this. Thanks.

您没有提供示例输入/输出,但我认为您正在寻找类似的东西

var result = yourMainDict.OrderBy(x => x.Value.Sum(v => v.Value.Delivered + v.Value.Submitted + v.Value.Failed));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM