using System.Collections.Generic;
public static void Main()
var model = new Dictionary<string, Dictionary<string, Model>>
new Dictionary<string, Model>
{"SubKey1", new Model { Time = new DateTime(2020, 09, 15)}},
{"SubKey2", new Model { Time = new DateTime(2020, 12, 15) }}
new Dictionary<string, Model>
{"SubKey1", new Model { Time = new DateTime(2020, 11, 15) }},
var result = model.Values.SelectMany(x => x).GroupBy(x => x.Key).ToDictionary(x => x.Key, x => x.Select(m => m.Value).MinBy(m => m.Time));
public DateTime Time { get; set; }
public override string ToString() => Time.ToString();
public static class EnumerableExtensions
public static TSource MinBy<TSource, TKey>(
this IEnumerable<TSource> source,
Func<TSource, TKey> selector,
IComparer<TKey> comparer = null)
if (source == null) throw new ArgumentNullException(nameof(source));
if (selector == null) throw new ArgumentNullException(nameof(selector));
comparer ??= Comparer<TKey>.Default;
using (var sourceIterator = source.GetEnumerator())
if (!sourceIterator.MoveNext())
throw new InvalidOperationException("Sequence contains no elements");
var min = sourceIterator.Current;
var minKey = selector(min);
while (sourceIterator.MoveNext())
var candidate = sourceIterator.Current;
var candidateProjected = selector(candidate);
if (comparer.Compare(candidateProjected, minKey) < 0)
minKey = candidateProjected;