簡體   English   中英

如何遍歷包含字典的字典,該字典包含另一個包含列表的字典

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

我想改變列表中對象的值。 List本身位於字典中,該字典位於字典中。 我是否必須分別遍歷每一個? 我嘗試了多個foreachs,但是我沒有成功。

這是字典:

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

我個人更喜歡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
}
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
             }

這應該工作

使用像這樣的復雜結構可能會變得尷尬和令人沮喪。 我建議您創建自己的繼承自此結構的類,以封裝用於在結構中添加數據並枚舉其條目的邏輯。 這是一個基本的實現:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM