using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using LanguageFeatures.Models; namespace LanguageFeatures.Controllers { public class HomeController : Controller { // GET: Home public string Index() { return "Navigate to a URL to show an example"; } public ViewResult AutoProperty() { Product myProduct = new Product(); myProduct.Name = "Kayak"; string productName = myProduct.Name; return View("Result", (object)string.Format("Product name: {0}", productName)); } public ViewResult CreateProduct() { Product myProduct = new Product { ProductID = 100, Name = "Kayak", Description = "A boat for one person", Price = 275M, Category = "Watersports" }; return View("Result", (object)string.Format("Category: {0}", myProduct.Category)); } public ViewResult CreateCollection() { string[] stringArray = { "apple", "oragne", "plum" }; List intList = new List { 10, 20, 30, 40 }; Dictionary myDict = new Dictionary { { "apple", 10 }, { "orange", 20 }, { "Plum", 30 } }; return View("Result", (object)stringArray[1]); } public ViewResult UseExtension() { ShoppingCart cart = new ShoppingCart { Products = new List { new Product {Name = "Kayak", Price = 275M}, new Product {Name = "Lifejacket", Price = 48.95M }, new Product {Name = "Soccer ball", Price = 19.50M }, new Product {Name = "Corner flag", Price = 34.95M } } }; decimal cartTotal = cart.TotalPrices(); return View("Result", (object)string.Format("Total: {0:c}", cartTotal)); } public ViewResult UseFilterExtensionMethon() { IEnumerable products = new ShoppingCart { Products = new List { new Product {Name = "Kayak", Category = "Watersports", Price = 275M }, new Product {Name = "Lifejacket", Category = "Watersports", Price = 48.95M}, new Product {Name = "Soccer ball", Category = "Soccer", Price = 19.50M }, new Product {Name = "Coner flag", Category = "Soccer", Price = 34.95M } } }; Func categoryFileter = delegate (Product prod) { return prod.Category == "Soccer"; }; decimal total = 0; foreach (Product prod in products.Filter(categoryFileter)) { total += prod.Price; } return View("Result", (object)String.Format("Total: {0}", total)); } } }