gpt4 book ai didi

c# Dictionary> 按日期时间排序列表(可为空值)

转载 作者:太空宇宙 更新时间:2023-11-03 23:31:17 24 4
gpt4 key购买 nike

我有一个 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 可为空(最新的优先)在 Dictionary 中使用 OrderByDescending 列表?

最佳答案

我想我明白了:

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

关于c# Dictionary<DateTime, List<Item>> 按日期时间排序列表(可为空值),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32153541/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com