3

I have these objects:

public class Class
{
     public Guid Id {get;set;}
     public string Name {get;set;}
     public virtual ICollection<Schedule> Schedules{get;set;}
}

public class Schedule
{
     public Guid Id {get;set;}
     public virtual DayOfTheWeekId {get;set;}
     public virtual DayOfTheWeek {get;set;}
     public DateTime StartTime {get;set;}
     public DateTime EndTime {get;set;}
}

My current query looks like this, but I get this exception: At least one object must implement IComparable.:

    Repository
.Get(c => c.Schedules.Any(s => s.DayOfTheWeekTypeId == dayOfTheWeekId))
.OrderBy(e => e.Schedules.OrderBy(s => s.StartDateTime)).ToList()

when I set the times i always use the same day, because I need to show classes on certain days of the week. That is where the DayOfTheWeek object comes into play. This is how I am setting the times:

var schedule = new Schedule{
                           StartDateTime = new DateTime(1999,1,1) + new TimeSpan(9, 15, 0),
                           EndDateTime = new DateTime(1999,1,1) + new TimeSpan(9, 15, 0),
                           DayOfTheWeekTypeId = 1
                           }

Update:

Thinking about this, I guess I may want grouping...

DDiVita
  • 4,225
  • 5
  • 63
  • 117

1 Answers1

8

You're trying to order by a sequence. What does that even mean?

It would make more sense to order by - say - the earliest event in the schedule:

.OrderBy(e => e.Schedules.Min(s => s.StartDateTime))

I don't know whether that will work, but it at least makes more sense. (It would work in LINQ to Objects. I have no idea about EF though.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • that is exactly what I needed, but I just realized that I am going to pull back all schedules regardless of day of the week. I'll have to modify my query so I am only pulling back the schedules for a given day. – DDiVita Jun 14 '12 at 20:53
  • once again you saved a life! – SHM Mar 17 '18 at 11:12