【问题标题】:How to sort dictionary by DateTime attribute of the value如何按值的 DateTime 属性对字典进行排序
【发布时间】:2012-05-18 09:13:42
【问题描述】:

这是我的尝试

public MyClass
{
   public Guid ID {set; get;}
   public DateTime CreatedDateTime {set; get;}
}


ConcurrentDictionary<Guid, MyClass> dic = new ConcurrentDictionary<Guid,MyClass>();

我试试这个,但我遇到了一些麻烦......

dic.OrderBy(i=>i.Value.CreatedDateTime);

我遇到了类似的错误

Cannot implicitly convert type 'System.Linq.IOrderedEnumerable'

有什么线索吗?谢谢!!

【问题讨论】:

  • 对字典进行排序真的没有意义......值不是这样存储的
  • @Muad'Dib 那么我该如何对这个字典进行排序呢?你能帮忙吗?
  • dictionary 没有实现 IOrderdEnumerable,所以你不能在它上面使用 OrderBy()。字典中的项目是按任意顺序存储的,没有“第一个”元素和“最后一个”元素的概念,所以你不能真正对它进行排序

标签: c# .net datetime sorting dictionary


【解决方案1】:

试试dic.Values.OrderBy(i=&gt;i.CreatedDateTime)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-20
    • 2019-01-25
    • 1970-01-01
    • 1970-01-01
    • 2016-04-16
    • 2010-10-11
    相关资源
    最近更新 更多