venter
venter

Reputation: 2090

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.

Upvotes: 2

Views: 2488

Answers (2)

Harald Coppoolse
Harald Coppoolse

Reputation: 30474

It seems you don't try to order your sequence of dispoLinien in ascending order of Beginn of Fahrt, but in ascending order of Beginn Fahrt sequences. You haven't defined the comparison between two Beginn Fahrt Sequences.

To demonstrate this, I've split your difficult OrderBy statement into small steps where I check the type of each part of this OrderBy. Let's do this for the first element of your dispoLinien:

var dl = dispoLinien.First(); var linienVersion = dl.Linie.GetValidLinienVersionByDate(date);

You haven't told the type of the linienVersion, but the returned linienVersion has a property Fahrten of a type that implements IEnumerable. In other words: from a dl and a date you can get a linienVersion that as a sequence of Fahrts.

IEnumerable<Fahrt> fahrten = linienVersion.Fahrten.
var fahrtBeginns = fahrten
    .Select(fahrt => fahrt.Beginn);

So from each dl you extract a collection of Beginns of Fahrts.

The result is that you want to order your collection by an ascending order of a collection of Beginns of Fahrts.

I'm pretty sure that this is not what you want. If you want to order your dispLinien in an ascending order of Beginn Fahrts, you should for every dl get the earliest of all Fahrts:

var orderedDispoLinien = dispoLinien.OrderBy(dispoLinie => 
    dispoLinie.Linie.GetValidLinienVersionByDate(date)
        .Fahrten                       // get only Fahrten
        .Where(fahrt => fahrt.Any()    // that are not empty
        .Select(fahrt => fahrt.Beginn) // from each fahrt take Beginn
        .Min(beginn => beginn));       // and use the earliest Beginn Fahrt

Now you have sorted your dispoLinien in ascending order of smallest Beginn Fahrt, meaning that the dispoLinie with the earliest Abfahrt is first.

GroupBy does not specify the order of the groups in the result, so it's not meaningful to OrderBy before the Grouping. If you want an ordered result you should OrderBy after the GroupBy

Upvotes: 0

selami
selami

Reputation: 2498

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

Upvotes: 1

Related Questions