0

This is the expected result: The accumulatedMaxPax should be equal to the previous value + the current maxPax. It should reset (=0) on every destinations change.

[
    {
        "date": "2022-05-06",
        "destinations": [
            {
                "id": 1,
                "description": "PAXOS - ANTIPAXOS",
                "abbreviation": "PA",
                "ports": [
                    {
                        "id": 1,
                        "description": "CORFU PORT",
                        "maxPax": 200,
                        "accumulatedMaxPax": 200 // Initial (accumulatedMaxPax = maxPax )
                    },
                    {
                        "id": 4,
                        "description": "BENITSES PORT",
                        "maxPax": 200,
                        "accumulatedMaxPax": 400 // 200 (previous) + 200 (current) = 400
                    },
                    {
                        "id": 17,
                        "description": "BOUKARI PORT",
                        "maxPax": 100,
                        "accumulatedMaxPax": 500 // 400 (previous) + 100 (current) = 500
                    },
                    {
                        "id": 2,
                        "description": "LEFKIMMI PORT",
                        "maxPax": 400,
                        "accumulatedMaxPax": 900 // 500 (previous) + 400 (current) = 900
                    }
                ]
            },
            {
                "id": 3,
                "description": "BLUE LAGOON",
                "abbreviation": "BL",
                "ports": [
                    {
                        "id": 1,
                        "description": "CORFU PORT",
                        "maxPax": 215,
                        "accumulatedMaxPax": 215
                    },
                    {
                        "id": 4,
                        "description": "BENITSES PORT",
                        "maxPax": 215,
                        "accumulatedMaxPax": 430
                    }
                ]
            },
            {
                "id": 7,
                "description": "PARTY",
                "abbreviation": "PRT",
                "ports": [
                    {
                        "id": 1,
                        "description": "CORFU PORT",
                        "maxPax": 200,
                        "accumulatedMaxPax": 200
                    }
                ]
            }
        ]
    }
]

This is the code which works according to the specs, with the missing part of the puzzle as question marks!

public IEnumerable<AvailabilityCalendarGroupVM> GetForCalendar(string fromDate, string toDate) {
    return context.Schedules
        .Where(x => x.Date >= Convert.ToDateTime(fromDate) && x.Date <= Convert.ToDateTime(toDate))
        .GroupBy(x => x.Date)
        .Select(x => new AvailabilityCalendarGroupVM {
            Date = DateHelpers.DateTimeToISOString(x.Key.Date),
            Destinations = x.GroupBy(x => new { x.Date, x.Destination.Id, x.Destination.Description, x.Destination.Abbreviation }).Select(x => new DestinationCalendarVM {
                Id = x.Key.Id,
                Description = x.Key.Description,
                Abbreviation = x.Key.Abbreviation,
                Ports = x.GroupBy(x => new { x.PortId, x.Port.Description, x.Port.Abbreviation, x.MaxPax, x.Port.StopOrder }).OrderBy(x => x.Key.StopOrder).Select(x => new PortCalendarVM {
                    Id = x.Key.PortId,
                    Description = x.Key.Description,
                    MaxPax = x.Key.MaxPax,
                    AccumulatedMaxPax = NOW WHAT ??? 
                })
            })
        }).ToList();
}
  • The `GroupBy` statement seems to include a lot of columns. Are all these fields necessary? – WisdomSeeker Oct 09 '22 at 15:43
  • If I don't include them, the new `DestinationCalendarVM` can't be created: If for example I omit the `x.Destination.Description`, the code won't compile with the error: **' does not contain a definition for 'Description'** The same applies for the `PortCalendarVM` – John Sourvinos Oct 09 '22 at 17:10
  • 1
    There's not an easy method to get a built in rolling sum. You can abuse the `Aggregate` extension (~ https://stackoverflow.com/a/5175351/1462295 ), but probably just iterate the collection and sum it manually. `foreach (var d in Destinations) { foreach (var p in d.Ports) { ...` – BurnsBA Oct 09 '22 at 17:41

0 Answers0