public void Update(Order order, List <OrderInfo> OrderInfo) { Order org = Get(order.ID); if (org == null) { throw new Exception($"Order ({order.ID}) is Not Exist"); } var orgOIs = Get_Data.GetOIs_ByOrder(order.ID); var uc = new MCategories().GetUNCategory().ID; var up = new MProducers().GetUNProducer().ID; foreach (var item in orgOIs) { var ori = Get_Data.GetOrderInfo(item.Order_ID, item.Product_ID); var pi = OrderInfo.Where(i => i.Product_ID == item.Product_ID).FirstOrDefault(); var product = Get_Data.Get_Product(item.Product_ID); if (pi != null) { pi.ID = ori.ID; pi.Order_ID = order.ID; if (pi.Quantity != item.Quantity) { if (pi.Quantity > item.Quantity) { product.Quantity += (pi.Quantity - item.Quantity); } else if (pi.Quantity < item.Quantity) { product.Quantity -= (item.Quantity - pi.Quantity); } if (product.Quantity < 0) { product.Quantity = 0; } Management.Detach(ori); Management.Update(pi); } } else { product.Quantity -= item.Quantity; Management.Remove(item); } new MProducts().Update(product); } foreach (var item in OrderInfo.Where(i => String.IsNullOrEmpty(i.Order_ID)).ToList()) { Management.Add(new OrderInfo() { Order_ID = order.ID, Product_ID = item.Product_ID, Price = item.Price, Quantity = item.Quantity }); Product product = Get_Data.Get_Product(item.Product_ID); product.Quantity += item.Quantity; new MProducts().Update(product); } }