I have common subkeys (SubKey1/SubKey2, etc) for each set of BaseKey1, BaseKey2, .... etc, but occurrence of all subkeys for each basekey is NOT fixed. In below example "SubKey2" not present for "BaseKey2".
var model = new Dictionary<string, Dictionary<string, Model>>
{
{
"BaseKey1",
new Dictionary<string, Model>
{
{"SubKey1", new Model { Time = new DateTime(2020, 09, 15)}},
{"SubKey2", new Model { Time = new DateTime(2020, 12, 15) }}
}
},
{
"BaseKey2",
new Dictionary<string, Model>
{
{"SubKey1", new Model { Time = new DateTime(2020, 11, 15) }},
}
}
};
I need to pull Min
Time for each subkey
from all basekey
and to do so I am doing below,
var query = model.Values
.SelectMany(d => d.Keys)
.Distinct()
.Select(key => new
{
Key = key,
Time = model.Values.Min(v => v[key].Time)
})
.ToList();
But it's giving error "The given key was not present in the dictionary" due to "SubKey2" not present for "BaseKey2". What could be solution here? I need below output. Thanks!