Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7834853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T13:21:38+00:00 2026-06-02T13:21:38+00:00

I have this two entities: public class Course { public int Id { get;

  • 0

I have this two entities:

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 Class grouping by Course and ordered by (descending) Course.StartDate then by Class.StartTime.
I can get to group by Course and order by Course.StartDate:

var myList = context.Classes
    .GroupBy(c => c.Course)
    .OrderByDescending(g => g.Key.StartDate).ToList()

But can’t manage to also order each Class by it’s StartTime. I tried this:

var myList = context.Classes
    .OrderBy(c => c.StartTime)
    .GroupBy(c => c.Course)
    .OrderByDescending(g => g.Key.StartDate).ToList()

And even this:

var myList = context.Classes
    .GroupBy(c => c.Course)
    .OrderByDescending(g => g.Key.StartDate)
    .ThenBy(g => g.Select(c => c.StartTime)).ToList()

But the Classes are never ordered by it’s StartTime.

*Edit for better clarification:
This is for a WebService and I must return a List<IGrouping<Course, Class>> and I really want an elegant way of doing this (i.e. using just Linq), without manually creating the list. Unless it’s not possible of course.

Any help is apreciated (I’m using EF code-first 4.3 btw).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T13:21:40+00:00Added an answer on June 2, 2026 at 1:21 pm

    If you need to preserve the signature, I suggest the following:

    var myList = context.Classes
                        .GroupBy(cc => cc.Course)
                        .OrderByDescending(gg => gg.Key.StartDate)
                        .ToArray() // <- stops EF and starts L2O
                        .SelectMany(gg => gg.OrderBy(cc => cc.StartTime))
                        .ToLookup(cc => cc.Course, cc => cc)
                        .ToList();
    

    This results in a type of signature: List<IGrouping<Course, Class>> and has them properly ordered by StartTime. This relies on the behavior of ToLookup with respect to maintaining the order found and may be subject to change.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have two Entities. public class Category{ public virtual int Id{get;set;} public virtual
I have two entities like: public class Employee { public int Id { get;
For example I have two entities Class A { public Guid Id {get;set;} public
I have two JPA entities: @Entity public class TaskSchedule { ... private String name;
I have two entities: public class Product { [HiddenInput(DisplayValue=false)] public int ProductID { get;
I have following two entities public class User { public int UserId { get;
I have following two entities public class User { [Key] public int Id {
I have the following two entities: public class Field { public int FieldID {
I have these two entities. One for Employees: [Table(Name = Employees)] public class Employee
We have the following two entities with many-to-many association: @Entity public class Role {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.