foreach (var subCategory in max4saleViewModel.subCategoriesList) { if (subCategory.IsBHold == true) { subCategory.StartTime = max4sale.StartTime; subCategory.EndTime = max4sale.EndTime; Max4Sale sbMax4Sale = AutoMapper.Mapper.Map<Max4SaleList, Max4Sale>(subCategory); conflicts = GetConflicts(sbMax4Sale); if (conflicts > 0) { throw ThrowConflictsError(conflicts); } sbMax4Sale.BHoldParId = max4sale.Id; subCategoriesToAdd.Add(sbMax4Sale); } else { if (subCategory.Max4SaleId != 0) { subCategoriesToDelete.Add(AutoMapper.Mapper.Map<Max4SaleList, Max4Sale>(subCategory)); } } } } if (action=="Update") _max4saleAdapter.Update(max4sale); else _max4saleAdapter.Add(max4sale);So, when I am updating an existing parent row, I can set BHoldParId column in the loop to a value. However, when I add a new parent row the id value will only be set after the Add has been run and I don't want to run another foreach loop if there is a simpler LINQ approach.
if (action=="Update") _max4saleAdapter.Update(max4sale); else { _max4saleAdapter.Add(max4sale); subCategoriesToAdd.ForEach(x=>x.BHoldParId=max4sale.Id); }