OrderBy Parent object from a nested collection

Plain

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 received the error message "At least one object must implement IComparable."

I need to order TimeLine by Milestone.Time. The first project on the list will be the one that has the leraliest Time property in the Milestone collection.

Need help with a link.

+5
source share
1 answer

It looks like you want

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

In other words, for everyone, TimeLinefind the earliest milestone and use it to organize.

Of course, if the milestones are ok, you can use:

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

Both of them will not work if either TimeLinedoes not have control points.

+10
source

All Articles