public async Task DeleteDay(int id, bool removePings = false) { var day = await GetByIdAsync(id); var pings = new List <Ping>(); // Find the previous day var previousDay = await GetPrevious(day); // Merge with the previous day if (previousDay is Day) { // Up the end time of the previous record previousDay.TimeTo = day.TimeTo; Update(previousDay); var rideId = previousDay.RideId; var locationId = previousDay.LocationId; var dayId = previousDay.Id; // Update the pings of the to be deleted day to the location of the previous day pings = await pingRepository.GetPings(day); foreach (var ping in pings) { if (!removePings) { ping.RideId = rideId; ping.LocationId = locationId; ping.DayId = dayId; pingRepository.Update(ping); } else { pingRepository.Delete(ping.Id); } } await SaveAsync(); // Check if the previous record is a ride. This also needs to be extended if (previousDay.Ride is Ride) { previousDay.Ride.TimeTo = day.TimeTo; rideRepository.Update(previousDay.Ride); var nextDay = await GetNext(day); // Is the next record also a ride, Merge them together. if (nextDay is Day && nextDay.Ride is Ride) { previousDay.TimeTo = nextDay.TimeTo; previousDay.Ride.TimeTo = nextDay.TimeTo; previousDay.Ride.DistanceInMeters = null; pings = await pingRepository.GetPings(nextDay); foreach (var ping in pings) { ping.RideId = rideId; ping.LocationId = locationId; ping.DayId = dayId; pingRepository.Update(ping); } rideRepository.Delete(nextDay.Ride.Id); Delete(nextDay.Id); } previousDay.Ride.ResetDistance(); rideRepository.Update(previousDay.Ride); } Update(previousDay); await SaveAsync(); if (day.Ride is Ride) { // The previous merge actions removed all pings. // But in case any pings didn't get moved double check by selecting with the ride instead of the day. pings = await pingRepository.GetPings(day.Ride); foreach (var ping in pings) { if (!removePings) { ping.RideId = rideId; ping.LocationId = locationId; ping.DayId = dayId; pingRepository.Update(ping); } else { pingRepository.Delete(ping.Id); } } rideRepository.Delete(day.Ride.Id); } } Delete(day.Id); await SaveAsync(); }