c#Dictionary <datetime,list <item =“”>&gt;按日期排序的订单列表(可空值)

时间:2015-08-22 07:27:21

标签: c#

我有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也列入字典中的列表?

2 个答案:

答案 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());