public static void checkAndSaveServiceJobs(List <ServiceJob> newServiceJob, List <ServiceJob> oldServiceJob, PodaciContext db) { if (newServiceJob == null) { newServiceJob = new List <ServiceJob>(); } if (oldServiceJob == null) { oldServiceJob = new List <ServiceJob>(); } List <ServiceJob> toRemove = new List <ServiceJob>(); foreach (var job in oldServiceJob) { if (!newServiceJob.Any(x => x.ServiceId == job.ServiceId)) { toRemove.Add(job); } } if (toRemove.Count > 0) { db.RemoveRange(toRemove); db.SaveChanges(); } foreach (var job in newServiceJob) { if (!oldServiceJob.Any(x => x.ServiceId == job.ServiceId)) { db.Add(job); db.SaveChanges(); //job.CleaningJob = null; } } }