1

I have a 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());

This will take a List<Item> groups it per year/month, order it descending (newest first) then creates a dictionary out of it.

My Question is: how can I OrderByDescending also the List within the Dictionary based on the the DateTime nullable (newest first)?

Enigmativity
  • 113,464
  • 11
  • 89
  • 172
user2818430
  • 5,853
  • 21
  • 82
  • 148

2 Answers2

2

I think I figured it out:

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());
user2818430
  • 5,853
  • 21
  • 82
  • 148
1

Try this:

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());
Enigmativity
  • 113,464
  • 11
  • 89
  • 172