public int AddOrder(Order order) { int maxId = WebSiteDB.MyNorthwind.Orders.Max(x => x.OrderID); order.OrderID = maxId + 1; if( order.CustomerID > 0 ) order.CustomerName = WebSiteDB.MyNorthwind.Customers.Find(c => c.CustomerID == order.CustomerID).CustomerName; // 为每个订单明细记录设置【商品属性】,累加订单总金额。 for( int i = order.Details.Count - 1; i >= 0; i-- ) { OrderDetail detail = order.Details[i]; Product product = WebSiteDB.MyNorthwind.Products.FirstOrDefault(p => p.ProductID == detail.ProductID); if( product != null ) { detail.UnitPrice = product.UnitPrice; detail.ProductName = product.ProductName; detail.Unit = product.Unit; order.SumMoney += detail.UnitPrice * detail.Quantity; } else { order.Details.Remove(detail); } } WebSiteDB.MyNorthwind.Orders.Add(order); return order.OrderID; }
public Order ConvertToOrderItem() { // 验证对象是否有效 string error = this.IsValid(); if( string.IsNullOrEmpty(error) == false ) throw new MyMessageException(error); // 创建实体对象 Order order = new Order(); DateTime now = DateTime.Now; order.OrderDate = new DateTime(this.OrderDate.Year, this.OrderDate.Month, this.OrderDate.Day, now.Hour, now.Minute, now.Second); if( this.CustomerID > 0 ) order.CustomerID = this.CustomerID; order.Comment = this.Comment; order.Details = new List<OrderDetail>(); // 将 id1=2;id2=3; 的格式字符串分解出来 List<KeyValuePair<string, string>> detail = this.OrderDetail.SplitString(';', '='); // 将列表转成订单明细 detail.ForEach(x => order.Details.Add(new OrderDetail { ProductID = int.Parse(x.Key), Quantity = int.Parse(x.Value) })); return order; }