简体   繁体   English

如何遍历包含字典的字典,该字典包含另一个包含列表的字典

[英]How can I iterate through a dictionary that contains a dictionary that contains another dictionary which contains a list

I wanna change the value of the objects that are in a list. 我想改变列表中对象的值。 The List itself is in a dictionary, which is in a dictionary, which is in a dictionary. List本身位于字典中,该字典位于字典中。 Do I have to iterate through each of them separately? 我是否必须分别遍历每一个? I tried it with multiple foreachs, however I wasnt successful. 我尝试了多个foreachs,但是我没有成功。

This is the Dictionary: 这是字典:

public static Dictionary<int,Dictionary<int,Dictionary<int,List<EventObj>>>> events_list; // <Year,<Month,<Day,Number of Events>>>

I personally prefer SelectMany for this kind of scenarios. 我个人更喜欢SelectMany用于这种场景。

IEnumerable<EventObj> eventObjects = events_list
    .SelectMany(x => x.Value)
    .SelectMany(x => x.Value)
    .SelectMany(x => x.Value);

foreach (EventObj eventObject in eventObjects)
{
    // change the object
}

Or as query syntax if you are into this kind of things.. ;) 或者作为查询语法,如果你遇到这种事情..;)

IEnumerable<EventObj> eventObjects =
    from years in events_list
    from months in years.Value
    from days in months.Value
    from events in days.Value
    select events;

foreach (EventObj eventObject in eventObjects)
{
    // change the object
}
foreach(var year in events_list.Keys)
    foreach(var month in events_list[year].Keys)
         foreach(var day in events_list[year][month].Keys)
             foreach(var eventObject in events_list[year][month][day])
             {
                 // do stuff
             }

This should work 这应该工作

Using a complex structure like this could become awkward and frustrating. 使用像这样的复杂结构可能会变得尴尬和令人沮丧。 I suggest that you create your own class that inherits from this structure, to encapsulate the logic for adding data in the structure and enumerating its entries. 我建议您创建自己的继承自此结构的类,以封装用于在结构中添加数据并枚举其条目的逻辑。 Here is a basic implementation: 这是一个基本的实现:

public class YearMonthDayTree<T>
    : Dictionary<int, Dictionary<int, Dictionary<int, List<T>>>>
{
    public void Add(int year, int month, int day, T item)
    {
        if (!this.TryGetValue(year,
            out Dictionary<int, Dictionary<int, List<T>>> yearDict))
        {
            yearDict = new Dictionary<int, Dictionary<int, List<T>>>(12);
            this[year] = yearDict;
        }
        if (!yearDict.TryGetValue(month, out Dictionary<int, List<T>> monthDict))
        {
            monthDict = new Dictionary<int, List<T>>(31);
            yearDict[month] = monthDict;
        }
        if (!monthDict.TryGetValue(day, out List<T> dayList))
        {
            dayList = new List<T>();
            monthDict[day] = dayList;
        }
        dayList.Add(item);
    }

    public IEnumerable<(int Year, int Month, int Day, T Item)> Flatten()
    {
        foreach (var yearEntry in this)
        {
            int year = yearEntry.Key;
            foreach (var monthEntry in yearEntry.Value)
            {
                int month = monthEntry.Key;
                foreach (var dayEntry in monthEntry.Value)
                {
                    int day = dayEntry.Key;
                    foreach (var item in dayEntry.Value)
                    {
                        yield return (year, month, day, item);
                    }
                }
            }
        }
    }
}

Usage example: 用法示例:

var events_list = new YearMonthDayTree<EventObj>();
events_list.Add(2019, 6, 21, new EventObj());
events_list.Add(2019, 6, 22, new EventObj());
events_list.Add(2019, 6, 22, new EventObj());
foreach (var entry in events_list.Flatten())
{
    Console.WriteLine($"{entry.Year}.{entry.Month}.{entry.Day} {entry.Item}");
}

Output: 输出:

2019.6.21 EventObj 2019.6.21 EventObj
2019.6.22 EventObj 2019.6.22 EventObj
2019.6.22 EventObj 2019.6.22 EventObj

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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