简体   繁体   中英

Sorting grouped query in chronological order in LINQ query

The following LINQ query obtains the previous 12 months data and aggregates it by month.

I'd like to order this in chronological order, with the current month being the most recent in the query.

What's the best way to achieve this?

public IEnumerable<EventMonthlySummaryMonthly> GetLastYearEventGrid()
        {

            DateTime currentDate = DateTime.Now.AddYears(-1).AddMilliseconds(1);

            var summary = from p in db.Events
                          where (p.StartDate > currentDate) && (p.StartDate != null)
                          let k = new
                          {
                              Month = p.StartDate.Month
                          }
                          group p by k into t
                          select new EventMonthlySummaryMonthly
                          {
                              Month = t.Key.Month,
                              EventsWhatsOn = t.Count(p => p.EventTypeId == 1),
                              EventsRegular = t.Count(p => p.EventTypeId == 2),
                              EventsExhibitions = t.Count(p => p.EventTypeId == 3),
                              EventsAll = t.Count(p => p.EventTypeId != null),
                          };

            return summary;
        }

By sorting on the max date from the grouped month Descending. If the range spans several years this will give you the months that contains the latest dates first.

orderby t.Max(p => p.StartDate) descending
select new EventMonthlySummaryMonthly
{
   Month = t.Key.Month,
   EventsWhatsOn = t.Count(p => p.EventTypeId == 1),
   EventsRegular = t.Count(p => p.EventTypeId == 2),
   EventsExhibitions = t.Count(p => p.EventTypeId == 3),
   EventsAll = t.Count(p => p.EventTypeId != null),
}
    public IEnumerable<EventMonthlySummaryMonthly> GetLastYearEventGrid()
    {

        DateTime currentDate = DateTime.Now.AddYears(-1).AddMilliseconds(1);

        var summary = (from p in db.Events
                      where (p.StartDate > currentDate) && (p.StartDate != null)
                      let k = new
                      {
                          Month = p.StartDate.Month
                      }
                      group p by k into t
                      select new EventMonthlySummaryMonthly
                      {
                          Month = t.Key.Month,
                          EventsWhatsOn = t.Count(p => p.EventTypeId == 1),
                          EventsRegular = t.Count(p => p.EventTypeId == 2),
                          EventsExhibitions = t.Count(p => p.EventTypeId == 3),
                          EventsAll = t.Count(p => p.EventTypeId != null),
                      }).OrderByDescending(item=>item.Month);

        return summary;
    }

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