public void AddToCart(Soup soup) { // Get the matching cart and soup instances var cartItem = storeDB.Carts.SingleOrDefault( c => c.CartId == ShoppingCartId && c.ProductId == soup.Id); if (cartItem == null) { // Create a new cart item if no cart item exists cartItem = new Cart { ProductId = soup.Id, CartId = ShoppingCartId, Count = 1, DateCreated = DateTime.Now }; storeDB.Carts.Add(cartItem); } else { // If the item does exist in the cart, then add one to the quantity cartItem.Count++; } // Save changes storeDB.SaveChanges(); }
public decimal GetTotal() { Soup soup = new Soup(); // Multiply price by count to get // the current price for each in the cart // sum all price totals to get the cart total decimal?total = (from cartItems in storeDB.Carts where cartItems.CartId == ShoppingCartId select(int?) cartItems.Count *soup.Price).Sum(); return(total ?? decimal.Zero); }