venter venter - 1 month ago 7
C# Question

At least one object must implement IComparable order by

As the title describes an error occurres after using

order by
.

The order by statment looks like this:

var fahrerGroups = dispoLinien.OrderBy(dl => dl.Linie.GetValidLinienVersionByDate(date).Fahrten.Select(f => f.Beginn)).GroupBy(dl => dl.GetValidDispolinienVersionByDate(date).Fahrer);


Date is always the current date.

I'm trying to order by start time of a service. As far as I found out the error occurres, because my statment returns an object type and object doesn't implement IComparable. The point I didn't understand is that I'm trying to sort a dateTime and not an object type.

Answer

Select returns IEnumarable

Maybe, you can select first element to sort your list.

I add FirstOrDefault;

var fahrerGroups = dispoLinien
      .OrderBy(dl => dl
         .Linie
         .GetValidLinienVersionByDate(date)
         .Fahrten
         .Select(f => f.Beginn)
         .FirstOrDefault())
      .GroupBy(dl => dl
         .GetValidDispolinienVersionByDate(date)
         .Fahrer);
Comments