I have a C# dictionary Dictionary<MyKey, MyValue>
and I want to split this into a collection of Dictionary<MyKey, MyValue>
, based on MyKey.KeyType
. KeyType
is an enumeration.
Then I would be left with a dictionary containing key-value pairs where MyKey.KeyType = 1
, and another dictionary where MyKey.KeyType = 2
, and so on.
Is there a nice way of doing this, such as using Linq?
If your home does not have ductwork, a split air conditioner is a good option since you will not incur the additional cost of having ductwork installed in every room. The single system works well within a small area providing adequate heating or cooling.
Vent between the two rooms using a top vent (one near the ceiling) and a lower vent (near the floor). Also a fan at the lower vent forcing the air from the air conditioned room to the non-air conditioned room.
A multi split-air conditioner is a type of split air conditioner. While a split air conditioner cools only one room at a time, a multi-split air conditioner lets you cool multiple rooms at a time.
Normally one indoor unit should be used for one room only, since each room has different needs. Also, people who live in two different rooms might have different comfort needs. This way the operating setting of the unit might satisfy the needs of the one room, but not of the second.
var dictionaryList =
myDic.GroupBy(pair => pair.Key.KeyType)
.OrderBy(gr => gr.Key) // sorts the resulting list by "KeyType"
.Select(gr => gr.ToDictionary(item => item.Key, item => item.Value))
.ToList(); // Get a list of dictionaries out of that
If you want a dictionary of dictionaries keyed by "KeyType" in the end, the approach is similar:
var dictionaryOfDictionaries =
myDic.GroupBy(pair => pair.Key.KeyType)
.ToDictionary(gr => gr.Key, // key of the outer dictionary
gr => gr.ToDictionary(item => item.Key, // key of inner dictionary
item => item.Value)); // value
I believe the following will work?
dictionary
.GroupBy(pair => pair.Key.KeyType)
.Select(group => group.ToDictionary(pair => pair.Key, pair => pair.Value);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With