コード例 #1
0
        public static void Seed(this productsContext dbContext)
        {
            dbContext.product.AddRange(
                new ProductDto()
            {
                Name = "galaxy bar", Price = 12.99, Stock = 42
            },
                new ProductDto()
            {
                Name = "20kg galaxy bar", Price = 12.99, Stock = 42
            },
                new ProductDto()
            {
                Name = "20kg galaxy bar", Price = 12.99, Stock = 42
            },
                new ProductDto()
            {
                Name = "20kg galaxy bar", Price = 12.99, Stock = 42
            }
                );

            dbContext.productHistory.AddRange(
                new ProductHistoryDto()
            {
                ProductId = 0, DateChange = DateTime.Now, Price = 12.99
            },
                new ProductHistoryDto()
            {
                ProductId = 1, DateChange = DateTime.Now, Price = 12.99
            },
                new ProductHistoryDto()
            {
                ProductId = 2, DateChange = DateTime.Now, Price = 12.99
            },
                new ProductHistoryDto()
            {
                ProductId = 3, DateChange = DateTime.Now, Price = 12.99
            }
                );

            dbContext.review.AddRange(
                new ReviewDto()
            {
                CustomerID = 0, ProductID = 32, Rating = 3, Comments = "fab", Visible = true
            },
                new ReviewDto()
            {
                CustomerID = 1, ProductID = 2, Rating = 5, Comments = "perfect", Visible = true
            },
                new ReviewDto()
            {
                CustomerID = 2, ProductID = 34, Rating = 1, Comments = "not to good", Visible = false
            }
                );

            dbContext.SaveChanges();
        }
コード例 #2
0
ファイル: catalogBO.cs プロジェクト: escuber/CTDI_CodeSample
        public IEnumerable <catalog> get_productsWithDiscountsFilteredByDate()
        {
            if (!_db.product.Any())
            {
                _db.product.AddRange(ProductDataGenerator.generateProductsDataList());
                _db.SaveChanges();
            }
            // here we are going to generate the output query twice because it gets
            // really ugly and hard to read otherwise
            // the first time we are setting the discountpercent and the second time we
            // we are using the value to set the price.

            //  the good thing is that is only happens once because of the query it is writting

            var return_products = _db.product.Select(p => new catalog
            {
                productName        = p.productName,
                productDescription = p.productDescription,
                productId          = p.productId,
                productPrice       = p.productPrice,
                imageName          = p.imageName,
                discountPercent    =
                    p.productDiscount.Any(pd => pd.startDate <= DateTime.Now && pd.endDate > DateTime.Now)
                 ?
                    Convert.ToDecimal(
                        p.productDiscount.Where(pd => pd.startDate <= DateTime.Now && pd.endDate > DateTime.Now).OrderBy(pd => pd.discount.discount_percent).FirstOrDefault().discount.discount_percent) * new decimal(.01)
                 : 0,
            }).AsQueryable();

            return(return_products.Select(p => new catalog
            {
                productName = p.productName,
                productDescription = p.productDescription,
                productId = p.productId,
                productPrice = p.productPrice,
                imageName = p.imageName,
                discountPercent = p.discountPercent,
                finalPrice = p.productPrice - Math.Round(p.discountPercent * p.productPrice, 2)
            }).AsQueryable());
        }