public static void Merge(this City city, City newData, ObservableCollection<ParkingLot> parkingLotCollection) { if (newData == null || parkingLotCollection == null) { return; } Task.Run(async () => { await DispatcherHelper.RunAsync(() => { city.DataSource = newData.DataSource; city.LastDownloaded = newData.LastDownloaded; city.LastUpdated = newData.LastUpdated; }); }); Parallel.ForEach(newData.Lots, async (newLot) => { await DispatcherHelper.RunAsync(() => { var existingLot = city.Lots.FirstOrDefault(x => x.Id == newLot.Id); if (existingLot == null) { city.Lots.Add(newLot); parkingLotCollection.Add(newLot); } else { existingLot.TotalLots = newLot.TotalLots; existingLot.FreeLots = newLot.FreeLots; existingLot.Address = newLot.Address; existingLot.HasForecast = newLot.HasForecast; existingLot.Coordinates = newLot.Coordinates; existingLot.LotType = newLot.LotType; existingLot.Forecast = newLot.Forecast; existingLot.HasLongForecast = newLot.HasLongForecast; existingLot.Name = newLot.Name; existingLot.Region = newLot.Region; existingLot.State = newLot.State; } }); }); Parallel.ForEach( city.Lots.Where( existingLot => newData.Lots.FirstOrDefault(newLot => newLot.Id == existingLot.Id) == null).ToList(), async (existingLot) => { await DispatcherHelper.RunAsync(() => { city.Lots.Remove(existingLot); var selectableParkingLot = parkingLotCollection.FirstOrDefault(x => x.Id == existingLot.Id); if (selectableParkingLot != null) { parkingLotCollection.Remove(selectableParkingLot); } }); }); }
private ParkingLot FindParkingLotById(City city, string id) { return city?.Lots?.FirstOrDefault(x => x.Id.Equals(id)); }