【问题标题】:At least one object must implement IComparable in Max()至少一个对象必须在 Max() 中实现 IComparable
【发布时间】:2015-10-01 22:36:44
【问题描述】:

谁能帮我解释一下为什么会出错

至少一个对象必须实现 icomparable

来自下面的代码?

item.IndicatorMeasurements
    .OrderBy(a => a.EntryDateRange.ToDateTime)
    .Max()
    .Color;

【问题讨论】:

标签: c# linq


【解决方案1】:

因为您试图找到IndicatorMeasurements 列表的最大值,而编译器不知道如何做到这一点。如果您想要具有最新日期的对象,您可以实现该接口,或者更容易,如果列表不太长,您可以使用

OrderBy(a => a.EntryDateRange.ToDateTime).Last()

OrderByDescending(a => a.EntryDateRange.ToDateTime).First().

如果列表很长,您应该实现该接口,或者使用您自己的方法来查找最大项。感谢 Andrey 的提示。

【讨论】:

  • 顺便说一句,OrderBy 的解决方案仅对找到最大值无效
猜你喜欢
  • 2021-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-01
  • 2015-08-10
相关资源
最近更新 更多