private void ResolveWorkOrderNotes(WorkOrderModel model) { var workOrderNotes = Mapper.Map <IEnumerable <SageNote> >(model.Notes); var workOrderFromMongo = _repository.SearchFor <SageWorkOrder>(x => x.WorkOrder == model.WorkOrder).Single(); if (workOrderNotes != null) { foreach (var workOrderNote in workOrderNotes) { if ((workOrderFromMongo.WorkNotes != null && workOrderFromMongo.WorkNotes.Contains(workOrderNote))) { this._sageApiProxy.EditNote(workOrderNote); } else { if (workOrderNote.NOTENBR == 0) { this._sageApiProxy.AddNote(workOrderNote); } else { this._sageApiProxy.EditNote(workOrderNote); } } } if (workOrderFromMongo.WorkNotes != null) { var idsToRemove = new List <long>(); foreach (var woNote in workOrderFromMongo.WorkNotes) { if (!workOrderNotes.Select(x => x.NOTENBR).Contains(woNote.NOTENBR)) { idsToRemove.Add(woNote.NOTENBR); } } if (idsToRemove.Any()) { this._sageApiProxy.DeleteNotes(idsToRemove); } } } else { if (workOrderFromMongo.WorkNotes != null) { var idsToRemove = workOrderFromMongo.WorkNotes.Select(x => x.NOTENBR); if (idsToRemove.Any()) { _sageApiProxy.DeleteNotes(idsToRemove); } } } }