示例#1
0
        // Method to add an Item, we need the Product and Quantity
        public virtual void AddItem(Product product, int quantity)
        {
            // check if the product already exist in the line collection
            CartLine line = lineCollection
                            .Where(p => p.Product.ProductID == product.ProductID) // give me the product if there is one there already, that has the same ID as the product that I am trying to add.
                            .FirstOrDefault();                                    // if it is not found, it will return a new CartLine item

            // we need to test
            if (line == null) // this will not return anything because it is null, so we know we need to add to the collection
            {
                // code to Add new line to the CartLine
                lineCollection.Add(new CartLine
                {
                    Product  = product,
                    Quantity = quantity
                });
            }
            // if it finds a cartLine already, we just get that line and we add the quantity that is being added. it is automatically 1
            else
            {
                line.Quantity += quantity;
            }
        }
示例#2
0
        public virtual void AddItem(Product product, int quantity)
        {
            // добавлял ли пользователь хотя бы один такой товар в корзину?
            CartLine line = Lines
                            .Where(p => p.Product.ProductID == product.ProductID)
                            .FirstOrDefault();

            // если нет - формируем модель пункта корзины и добавляем
            // в список
            if (line == null)
            {
                Lines.Add(new CartLine
                {
                    Product  = product,
                    Quantity = quantity
                });
            }
            // иначе если такой товар уже встречается в корзине -
            // увеличиваем его количество в модели пункта корзины
            else
            {
                line.Quantity += quantity;
            }
        }