public ViewResult UseFilterExtensionNoFunc()
        {
            IEnumerable <Product> products = new ShoppingCartInterface()
            {
                Products = new List <Product>
                {
                    new Product {
                        Name = "K1", Price = 25.00M, Category = "River"
                    },
                    new Product {
                        Name = "K2", Price = 11.00M, Category = "Estuary"
                    },
                    new Product {
                        Name = "K3", Price = 29.00M, Category = "Lake"
                    }
                }
            };

            // it can point to any method, as long as the values, a method accepts and returns, match the delegate signature
            //using lambda expression without Func
            decimal totalFilter = 0;

            //extension method that filteres the collection
            foreach (Product product in products.Filter(product => product.Category == "Estuary"))
            {
                totalFilter += product.Price;
            }

            return(View("Result", (object)String.Format("My shopping cart total with filter without Func is {0}", totalFilter)));
        }
        public ViewResult UseFilterExtension()
        {
            IEnumerable <Product> products = new ShoppingCartInterface()
            {
                Products = new List <Product>
                {
                    new Product {
                        Name = "K1", Price = 23.00M, Category = "River"
                    },
                    new Product {
                        Name = "K2", Price = 26.00M, Category = "River"
                    },
                    new Product {
                        Name = "K3", Price = 29.00M, Category = "Lake"
                    }
                }
            };
            decimal totalFilter = 0;

            //extension method that filteres the collection, FilterByCategory
            foreach (Product product in products.FilterByCategory("Lake"))
            {
                totalFilter += product.Price;
            }

            return(View("Result", (object)String.Format("My shopping cart total with filter is {0}", totalFilter)));
        }
Пример #3
0
        public ActionResult Index()
        {
            ShoppingCartInterface cart = new ShoppingCartInterface(calc)
            {
                Products = products
            };
            decimal totalValue = cart.CalculateProductTotal();

            return(View(totalValue));
        }
        public ViewResult UseExtensionEnumerable()
        {
            IEnumerable <Product> products = new ShoppingCartInterface()
            {
                Products = new List <Product>
                {
                    new Product {
                        Name = "K1", Price = 23.00M
                    },
                    new Product {
                        Name = "K2", Price = 26.00M
                    },
                    new Product {
                        Name = "K3", Price = 29.00M
                    }
                }
            };

            //create and populate an array of Product objects
            Product[] productArray =
            {
                new Product {
                    Name = "K1", Price = 23.00M
                },
                new Product {
                    Name = "K2", Price = 26.00M
                },
                new Product {
                    Name = "K3", Price = 29.00M
                }
            };

            //extension method in use - TotalPrice()
            //due to switch to interface we can use it:
            //1. for objects enumerated by IEnumerable<Product>, incl instances of ShoppingCart
            //2. for array of Products
            decimal cartTotal  = products.TotalPrices();
            decimal arrayTotal = productArray.TotalPrices();

            return(View("Result", (object)String.Format("My shopping cart total is {0} that should equal to total from the array of products {1}", cartTotal, arrayTotal)));
        }