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

时间:2019-06-21 15:48:25

标签: c# list dictionary

我想更改列表中对象的值。列表本身在字典中,在字典中,在字典中。我是否必须分别遍历每个对象?我尝试了多个foreach,但没有成功。

这是字典:

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

3 个答案:

答案 0 :(得分:3)

在这种情况下,我个人更喜欢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
}

或者如果您喜欢这种东西,也可以作为查询语法。.;)

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
}

答案 1 :(得分:0)

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
             }

这应该有效

答案 2 :(得分:0)

使用这样的复杂结构可能会变得尴尬而令人沮丧。我建议您创建一个从该结构继承的类,以封装用于在结构中添加数据和枚举其条目的逻辑。这是一个基本的实现:

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

用法示例:

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

输出:

  

2019.6.21 EventObj
  2019.6.22 EventObj
  2019.6.22 EventObj