Elegant way to create a nested Dictionary in C#

前端 未结 8 1830
深忆病人
深忆病人 2021-01-31 04:57

I realized that I didn\'t give enough information for most people to read my mind and understand all my needs, so I changed this somewhat from the original.

8条回答
  •  梦谈多话
    2021-01-31 05:20

    Here's a solution using Linq:

    Dictionary> dict = things
        .GroupBy(thing => thing.Foo)
        .ToDictionary(fooGroup => fooGroup.Key,
                      fooGroup => fooGroup.ToDictionary(thing => thing.Bar,
                                                        thing => thing.Baz));
    

提交回复
热议问题