简体   繁体   中英

LINQ group by date descending, and order by time ascending

I have the following model:

public class A
{
  public int? Id {get;set;}
  public DateTime Start {get;set;}
  public DateTime End {get;set;}
}

Now I want to group all the entries by date, and sort them per date descending, and sorting per time ascending (using LINQ). Example output:

Jan 3. 2012
-- 10:00-11:00
-- 11:00-13:00
Jan 2. 2012
-- 10:00-11:00
-- 12:00-15:00
Jan 1. 2012
-- 9:00-10:00
-- 12:00-13:00

Is it possible to achieve this in LINQ-to-SQL or do I have to this after the query has executed (on the result list)?

You mean like this?

IEnumerable<A> sorted = listOfA.OrderByDescending(a => a.Start.Date)
                               .ThenBy(a => a.Start.TimeOfDay);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM