我有Dictionary<DateTime, List<Item>>
Dictionary<DateTime, List<Item>> result =
myList
.GroupBy(k => new DateTime(k.Created.Value.Year, k.Created.Value.Month, 1))
.OrderByDescending(k => k.Key)
.ToDictionary(k => k.Key, v => v.ToList());
每年每月需要一个List<Item>
组,按顺序递减(最新的第一组),然后创建一个字典。
我的问题是:我如何根据DateTime可空(最新的第一个)将OrderByDescending也列入字典中的列表?
答案 0 :(得分:2)
我想我明白了:
Dictionary<DateTime, List<Item>> result = myList.GroupBy(k => new DateTime(k.Created.Value.Year, k.Created.Value.Month, 1))
.OrderByDescending(k => k.Key)
.ToDictionary(k => k.Key, v => v.OrderByDescending(t => t.Published.GetValueOrDefault()).ToList());
答案 1 :(得分:1)
试试这个:
Dictionary<DateTime, List<Item>> result =
myList
.GroupBy(k => new DateTime(k.Created.Value.Year, k.Created.Value.Month, 1))
.OrderByDescending(k => k.Key)
.ToDictionary(k => k.Key, v => v.OrderByDescending(x => x.Created).ToList());