public void Synchronize(List <int> workorders) { //Insert New var existingWoIDs = Values.Select(x => x.WorkorderID).ToList(); foreach (int woID in workorders) { if (!existingWoIDs.Contains(woID)) { var swo = new SubcontractWorkorder() { SubcontractID = _subcontractID, WorkorderID = woID, }; _db.SubcontractWorkorders.Add(swo); } } //Delete if not in new list foreach (SubcontractWorkorder wo in Values) { if (!workorders.Contains(wo.WorkorderID)) { _db.Entry(wo).State = EntityState.Deleted; } } _db.SaveChanges(); }
private void UpdateSubContract(int subContractID, string sfProjectID, string firstSiteContact, string secondSiteContact, double?budget, string providedBy, string remarks, DateTime?dueDate, string rush, string requirement, string requirementAsOther, double?estimatedShippingCost, double?itemValue, string workOrderList) { try { var subContract = _db.SubContracts.Where(x => x.SubcontractID == subContractID).FirstOrDefault(); if (subContract != null) { if (firstSiteContact != null) { int val = CommonMethods.GetMISID(TableName.Customer_Contact, firstSiteContact, sfProjectID); if (val != 0) { subContract.ContactPerson1 = val; } } if (secondSiteContact != null) { int val = CommonMethods.GetMISID(TableName.Customer_Contact, secondSiteContact, sfProjectID); if (val != 0) { subContract.ContactPerson2 = val; } } if (budget != null) { subContract.Budget = (double)budget; } if (providedBy != null) { subContract.BudgetProvideBy = providedBy; } if (remarks != null) { subContract.Remark = remarks; } if (dueDate != null) { subContract.TargetDate = (DateTime)dueDate; } switch (rush) { case "Yes": subContract.IsRush = true; break; case "No": subContract.IsRush = false; break; default: break; } switch (requirement) { case "Installation": subContract.RequirementID = 5; subContract.Requirement = requirement; break; case "Site Check": subContract.RequirementID = 10; subContract.Requirement = requirement; break; case "Service": subContract.RequirementID = 15; subContract.Requirement = requirement; break; case "Electrical Hook Up": subContract.RequirementID = 20; subContract.Requirement = requirement; break; case "Other": subContract.RequirementID = 65531; if (requirementAsOther != null) { subContract.Requirement = requirementAsOther; } else { subContract.Requirement = ""; } break; default: break; } if (estimatedShippingCost != null) { subContract.EstimatedShippingCost = estimatedShippingCost; } if (itemValue != null) { subContract.ItemValue = itemValue; } _db.Entry(subContract).State = EntityState.Modified; _db.SaveChanges(); } List <int> processedItems = new List <int>(); if (!string.IsNullOrEmpty(workOrderList)) { char[] delimiters = new char[] { '\n', ' ', ',' }; string[] items = workOrderList.Split(delimiters); if (items.Length != 0) { foreach (string e in items) { var workOrder = _db1.Sales_JobMasterList_WO.Where(x => x.WorkorderNumber == e).FirstOrDefault(); if (workOrder != null) { int rowId = 0; var subContractWO = _db.SubcontractWorkorders.Where(x => x.SubcontractID == subContractID && x.WorkorderID == workOrder.woID).FirstOrDefault(); if (subContractWO == null) { // not exist, add one row SubcontractWorkorder scWO = new SubcontractWorkorder(); scWO.SubcontractID = subContractID; scWO.WorkorderID = workOrder.woID; _db.SubcontractWorkorders.Add(scWO); _db.SaveChanges(); rowId = scWO.RowID; } else { rowId = subContractWO.RowID; } /* add it to know that it still exists */ processedItems.Add(rowId); } } } } /* delete all items which are not in the processedItems list */ var records = _db.SubcontractWorkorders.Where(x => x.SubcontractID == subContractID).ToList(); if (records.Any()) { foreach (var r in records) { if (processedItems.IndexOf(r.RowID) == -1) { //delete it _db.SubcontractWorkorders.Remove(r); } } _db.SaveChanges(); } } catch (Exception e) { LogMethods.Log.Error("UpdateSubContract:Error:" + e.Message); } }