2

Can anyone help me why I got error

at least one object must implement icomparable

from this below code?

item.IndicatorMeasurements
    .OrderBy(a => a.EntryDateRange.ToDateTime)
    .Max()
    .Color;
abatishchev
  • 98,240
  • 88
  • 296
  • 433
wiwiedbulu
  • 39
  • 1
  • 3
  • `ToDateTime` sounds like it's a method. Are you sure you didn't intend to call the method? (`a => a.EntryDateRange.ToDateTime()`) – theB Oct 01 '15 at 22:39
  • Possibly duplicate: http://stackoverflow.com/questions/2805703/good-way-to-get-the-key-of-the-highest-value-of-a-dictionary-in-c-sharp – Andrey Nasonov Oct 01 '15 at 22:40

1 Answers1

6

Because you are trying to find the max value of your list of IndicatorMeasurements, and the compiler has no idea of how to do that. If you want the object with the newest date, you either implement that interface or, more easily, and if the list is not too long, you can use

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

or

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

If the list is long, you should implement that interface or else use your own method to find the maximum item. Thanks Andrey for the hint.

Andrew
  • 7,602
  • 2
  • 34
  • 42