public static void SaveProductionJournalUpdate(List<ProductionJournal> myProductionJournal) { var db = new IstokDoorsDBContext(); //var dbProdJrnlList = db.ProductionJournals.ToList(); for (int i = 0; i < myProductionJournal.Count; i++) { var linqBatchId = myProductionJournal[i].BatchId; var linqFactoryId = myProductionJournal[i].FactoryId; var linqDoorId = myProductionJournal[i].DoorId; var dbItem = db.ProductionJournals.SingleOrDefault(b=>b.BatchId == linqBatchId && b.DoorId == linqDoorId && b.FactoryId == linqFactoryId); int? manufacturedDifference = myProductionJournal[i].IsManufacturedForBatch - dbItem.IsManufacturedForBatch; db.usp_UpdateAwaitingAllocQuantityForPlanningJournal(manufacturedDifference, dbItem.FactoryId, dbItem.BatchId, dbItem.DoorId); //Update InventoryJournal and InventoryBalance with Manufactured Quantities db.usp_UpdateInvenotriesWithManufacturedQuantities(dbItem.FactoryId, dbItem.DoorId, manufacturedDifference); //int quantityManufacuturedForBatch = myProductionJournal[i].IsManufacturedForBatch; //dbItem.IsManufacturedForBatch = 2; // quantityManufacuturedForBatch; db.SaveChanges(); } for (int i = 0; i < myProductionJournal.Count; i++) { var linqBatchId = myProductionJournal[i].BatchId; var linqFactoryId = myProductionJournal[i].FactoryId; var linqDoorId = myProductionJournal[i].DoorId; var dbItem = db.ProductionJournals.SingleOrDefault(b => b.BatchId == linqBatchId && b.DoorId == linqDoorId && b.FactoryId == linqFactoryId); int quantityManufacuturedForBatch = myProductionJournal[i].IsManufacturedForBatch; dbItem.IsManufacturedForBatch = quantityManufacuturedForBatch; } db.SaveChanges(); //for (int i = 0; i < myProductionJournal.Count; i++) //{ // foreach (var item in dbProdJrnlList) // { // if (myProductionJournal[i].BatchId == item.BatchId && myProductionJournal[i].DoorId == item.DoorId && myProductionJournal[i].IsManufacturedForBatch > item.IsManufacturedForBatch && myProductionJournal[i].FactoryId == item.FactoryId && myProductionJournal[i].DateOfBatchCompletion == null) // { // int? manufacturedDifference = myProductionJournal[i].IsManufacturedForBatch - item.IsManufacturedForBatch; // db.usp_UpdateAwaitingAllocQuantityForPlanningJournal(manufacturedDifference, // item.FactoryId, item.BatchId, item.DoorId); // //Update InventoryJournal and InventoryBalance with Manufactured Quantities // db.usp_UpdateInvenotriesWithManufacturedQuantities(item.FactoryId, item.DoorId, manufacturedDifference); // item.IsManufacturedForBatch = myProductionJournal[i].IsManufacturedForBatch; // //db.SaveChanges(); // } // } //} //for (int i = 0; i < myProductionJournal.Count; i++) //{ // for (int j = 0; j < dbProdJrnlList.Count; j++) // { // if (myProductionJournal[i].BatchId == dbProdJrnlList[j].BatchId && myProductionJournal[i].DoorId == dbProdJrnlList[j].DoorId && myProductionJournal[i].IsManufacturedForBatch > dbProdJrnlList[j].IsManufacturedForBatch && myProductionJournal[i].FactoryId == dbProdJrnlList[j].FactoryId && myProductionJournal[i].DateOfBatchCompletion == null) // { // int? manufacturedDifference = myProductionJournal[i].IsManufacturedForBatch - dbProdJrnlList[j].IsManufacturedForBatch; // db.usp_UpdateAwaitingAllocQuantityForPlanningJournal(manufacturedDifference, // myProductionJournal[i].FactoryId, myProductionJournal[i].BatchId, myProductionJournal[i].DoorId); // //Update InventoryJournal and InventoryBalance with Manufactured Quantities // db.usp_UpdateInvenotriesWithManufacturedQuantities(myProductionJournal[i].FactoryId, myProductionJournal[i].DoorId, manufacturedDifference); // //dbProdJrnlList[j].IsManufacturedForBatch = myProductionJournal[i].IsManufacturedForBatch; // var linqFactoryId = myProductionJournal[i].FactoryId; // var linqBatchId = myProductionJournal[i].BatchId; // var iinqDoorId = myProductionJournal[i].DoorId; // var batchToUpdate = db.ProductionJournals.SingleOrDefault(b => b.FactoryId == linqFactoryId && b.DoorId == iinqDoorId && b.BatchId == linqBatchId); // batchToUpdate.IsManufacturedForBatch = myProductionJournal[i].IsManufacturedForBatch; // db.SaveChanges(); // } // } }