“问题是”
这将按预期工作,并生成totalpax
字段正确的结果。但是,每个目标的pax
字段应该是基于目标ID的总和。
代码
public async Task<IEnumerable<ReservationCalendarGroupVM>> GetForCalendarAsync(string fromDate, string toDate) {
return await context.Schedules
.AsNoTracking()
.Where(x => x.Date >= Convert.ToDateTime(fromDate) && x.Date <= Convert.ToDateTime(toDate))
.GroupBy(z => new { z.Date })
.Select(e => new ReservationCalendarGroupVM {
Date = e.Key.Date.ToString(),
Destinations = e.GroupBy(i => new { i.Destination.Id, i.Destination.Abbreviation, i.Destination.Description }).Select(p => new DestinationCalendarVM {
Id = p.Key.Id,
Abbreviation = p.Key.Abbreviation,
Description = p.Key.Description,
Pax = context.Reservations.Where(y => y.Date == e.Key.Date).Sum(h => h.TotalPersons)
}),
TotalPax = context.Reservations.Where(y => y.Date == e.Key.Date).Sum(h => h.TotalPersons).ToListAsync();
}
结果
"date": "2022-07-02",
"destinations": [
{
"id": 1,
"description": "PAXOS - ANTIPAXOS",
"abbreviation": "PA",
"pax": 254
},
{
"id": 3,
"description": "BLUE LAGOON",
"abbreviation": "BL",
"pax": 254
}
],
"totalpax": 432
1条答案
按热度按时间jdzmm42g1#
我相信问题就在于这一行:
您只按日期过滤,但同时需要按目的地过滤。由于您没有共享模型,因此不太容易推断,但我认为您需要执行以下操作: