private void UpdateVehicleExtra(string[] selectedExtras, VehicleUser vehicleToUpdate) { if (selectedExtras == null) { vehicleToUpdate.Extras = new List<VehicleExtra>(); return; } var selectedExtrasHS = new HashSet<string>(selectedExtras); var VehicleExtra = new HashSet<int> (vehicleToUpdate.Extras.Select(c => c.ID)); foreach (var Extra in db.VehicleExtra) { if (selectedExtrasHS.Contains(Extra.ID.ToString())) { if (!VehicleExtra.Contains(Extra.ID)) { vehicleToUpdate.Extras.Add(Extra); } } else { if (VehicleExtra.Contains(Extra.ID)) { vehicleToUpdate.Extras.Remove(Extra); } } } }
private void PopulateExtraData(VehicleUser VehicleUser) { var VehicleExtras = db.VehicleExtra; var VehicleExtraVM = new List<AssignedVehicleExtra>(); var SelectedExtra = new HashSet<int>(VehicleUser.Extras.Select(e => e.ID)); foreach (var VehicleExtra in VehicleExtras) { VehicleExtraVM.Add(new AssignedVehicleExtra { ExtraID = VehicleExtra.ID, Description = VehicleExtra.Name, Assigned = SelectedExtra.Contains(VehicleExtra.ID) }); } ViewBag.VehicleExtras = VehicleExtraVM; }