I have two objects:
public class Course { public int Id { get; set; } public string Name { get; set; } public DateTime StartDate { get; set; } public DateTime EndDate { get; set; } public virtual ICollection<Class> Classes { get; set; } } public class Class { public int Id { get; set; } public string Name { get; set; } public DateTime StartTime { get; set; } public DateTime EndTime { get; set; } ] public Course Course { get; set; } }
Now I need to list each group of classes by course and sort by (descending) Course.StartDate, then by class .StartTime. I can get the course and order group Course.StartDate:
var myList = context.Classes .GroupBy(c => c.Course) .OrderByDescending(g => g.Key.StartDate).ToList()
But it is also not possible to order each class using StartTime. I tried this:
var myList = context.Classes .OrderBy(c => c.StartTime) .GroupBy(c => c.Course) .OrderByDescending(g => g.Key.StartDate).ToList()
And even that:
var myList = context.Classes .GroupBy(c => c.Course) .OrderByDescending(g => g.Key.StartDate) .ThenBy(g => g.Select(c => c.StartTime)).ToList()
But Classes are never ordered by StartTime.
* Edit for better clarification: This is for WebService, and I have to return List<IGrouping<Course, Class>>
, and I really need an elegant way to do this (for example, using only Linq), without manually creating the list. If this is not possible, of course.
Any help is appreciated (I use the first EF code 4.3 bit.).
source share