dotsa
dotsa

Reputation: 941

OrderBy Parent object by Nested Collection

Simple

public class TimeLine
{
  public String Name {get;set}
  public List<Milestone> MilesStones {get;set}
}

public class Milestone
{
  public String Name {get;set}
  public DateTime Time {get;set}
}

I tried: from t in DataAccess.TimelineCollection.OrderBy(c=>c.MilesStones.OrderBy(z=>z.MilestoneDate)) select t; but got an error that "At least one object must implement IComparable."

I need to order TimeLine by Milestone.Time. First project in a list will be the one that has eraliest Time property in Milestone collection.

Need help with link.

Upvotes: 7

Views: 5036

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1499760

It sounds like you might want

var query = DataAccess.TimelineCollection
                      .OrderBy(t => t.MileStones.Min(m => m.Time));

In other words, for each TimeLine, find the earliest milestone, and use that for ordering.

Of course if the milestones are in order, you could use:

var query = DataAccess.TimelineCollection
                      .OrderBy(t => t.MileStones.First().Time);

Both of these will fail if any TimeLine has no milestones.

Upvotes: 14

Related Questions