Can anyone tell me how can i do something like this without that dbContext(storeDB)

public decimal GetTotal()
{
decimal? total = (from cartItems in storeDB.Carts
                  where cartItems.CartId == ShoppingCartId
                  select (int?)cartItems.CartCount * cartItems.Product.ProductPrice).Sum();
return total ?? decimal.Zero;
}

i was trying to iterate trough list of carts(i get it trough another class with crud methods for cart model that i instantiated as scDAL) ,but than i get error like there is no relationship between Product and Cart models ...this is the error null referenceenter image description here
and those are models i use

public class Product
    {
        public int ProductID { get; set; }

        [Required, StringLength(100), Display(Name = "Name")]
        public string ProductName { get; set; }

        [Required, StringLength(1000), Display(Name = "Product Description"), DataType(DataType.MultilineText)]
        public string ProductDesc { get; set; }

        public string ProductImg { get; set; }

        [Display(Name = "Price"), DisplayFormat(DataFormatString = "{0:c}")]
        public decimal ProductPrice { get; set; }

        public int? CategoryID { get; set; }

        public virtual Category Category { get; set; }


    }

 public class Cart
    {
        public int RecordID { get; set; }

        public string CartID { get; set; }

        public int ProductID { get; set; }

        public int CartCount { get; set; }

        public System.DateTime DateCreated { get; set; }

        public virtual Product Product { get; set; }

    }

and this is class where my GetTotal() method is...

public class ShopingCart
    {
        ShopingCartDAL scDAL = new ShopingCartDAL();

        string ShopingCartID { get; set; }

        public const string CartSessionKey = "CartID";

        public static ShopingCart GetCart(HttpContextBase context)
        {
            var cart = new ShopingCart();
            cart.ShopingCartID = cart.GetCartID(context);
            return cart;
        }

        public void AddToCart(Product product)
        {
            var cartItem = scDAL.ReturnCarts().SingleOrDefault(
                c => c.CartID == ShopingCartID
                && c.ProductID == product.ProductID);

            if (cartItem == null)
            {
                cartItem = new Cart
                {
                    ProductID = product.ProductID,
                    CartID = ShopingCartID,
                    CartCount = 1,
                    DateCreated = DateTime.Now
                };
                scDAL.InsertCart(cartItem);
            }
            else cartItem.CartCount++;
         }

        public int RemoveFromCart(int id)
        {
            var cartItem = scDAL.ReturnCarts().Single(
                cart=>cart.CartID==ShopingCartID 
                && cart.RecordID==id);

            int itemCount = 0;
            if (cartItem!=null)
            {
                if (cartItem.CartCount>1)
                {
                    cartItem.CartCount--;
                    itemCount = cartItem.CartCount;
                }
                else
                {
                    scDAL.DeleteCart(cartItem);
                }
            }
            return itemCount;
        }

        public void EmptyCart()
        {
            var cartItems = scDAL.ReturnCarts().Where(
                cart => cart.CartID == ShopingCartID);

            foreach (var cartItem in cartItems)
            {
                scDAL.DeleteCart(cartItem);
            }
        }

        public List<Cart> GetCartItems()
        {
            return scDAL.ReturnCarts().Where(
                cart=>cart.CartID==ShopingCartID).ToList();
        }

        public int GetCount()
        {
            int? count = (from cartItems in scDAL.ReturnCarts()
                          where cartItems.CartID == ShopingCartID
                          select (int?)cartItems.CartCount).Sum();
            return count ?? 0;
        }

        public decimal GetTotal()
        {


            decimal? total = (from cartItems in scDAL.ReturnCarts()
                              where cartItems.CartID == ShopingCartID
                              select (int?)cartItems.CartCount * cartItems.Product.ProductPrice).Sum();

            return total ?? decimal.Zero;
        }

        public int CreateOrder(TheOrder order)
        {
            decimal orderTotal = 0;
            var cartItems = GetCartItems();

            foreach (var item in cartItems)
            {
                var orderDetail = new OrderDetail
                {
                    ProductID = item.ProductID,
                    TheOrderID = order.TheOrderID,
                    UnitPrice = item.Product.ProductPrice,
                    Quantity = item.CartCount
                };
                orderTotal += (item.CartCount*item.Product.ProductPrice);

                scDAL.InsertOrderDetail(orderDetail);
            }

            order.OrderTotal = orderTotal;
            scDAL.UpdateOrder(order);

            EmptyCart();

            return order.TheOrderID;
        }



        public string GetCartID(HttpContextBase context)
        {
            if (context.Session[CartSessionKey]==null)
            {
                if (!string.IsNullOrWhiteSpace(context.User.Identity.Name))
                {
                    context.Session[CartSessionKey] = context.User.Identity.Name;
                }
                else
                {
                    Guid tempCartID = Guid.NewGuid();

                    context.Session[CartSessionKey] = tempCartID.ToString();
                }
            }
            return context.Session[CartSessionKey].ToString();
        }
    }

Related posts

Recent Viewed