示例#1
0
        public ViewResult AutoProperty()
        {
            Product myProduct = new Product();
            myProduct.Name = "Kayak";
            string productName = myProduct.Name;

            return View("Result", (object)String.Format("Product name: {0}", productName));
        }
示例#2
0
        public ViewResult CreateProduct()
        {
            // create and populate a new Product object
            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}, Price: {1}", myProduct.Category, myProduct.Price));
        }
示例#3
0
        public ViewResult FindProducts()
        {
            Product[] products =
            {
                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 = "Corner flag", Category = "Soccer", Price = 34.95M}
            };

            // 1 - Without LINQ
            // define the array to hold the results
            Product[] foundProducts = new Product[3];

            // sort the contents of the array
            Array.Sort(products, (item1, item2) =>
            {
                return Comparer<decimal>.Default.Compare(item2.Price, item1.Price);
            });

            // get the first three items in the array as the results
            Array.Copy(products, foundProducts, 3);

            // create the result
            StringBuilder result1 = new StringBuilder();
            foreach (Product p in foundProducts)
            {
                result1.AppendFormat("Price: {0} ", p.Price);
            }

            // 2 - With LINQ query syntax
            var foundProducts2 = from match in products
                                orderby match.Price descending
                                select new
                                {
                                    match.Name,
                                    match.Price
                                };

            // create the result
            int count = 0;
            StringBuilder result2 = new StringBuilder();
            foreach (var p in foundProducts2)
            {
                result2.AppendFormat("Price: {0} ", p.Price);
                if (++count == 3)
                {
                    break;
                }
            }

            // 2 - With LINQ dot-notation syntax
            var foundProducts3 = products.OrderByDescending(e => e.Price)
                .Take(3)
                .Select(e => new {
                    e.Name,
                    e.Price
                });
            StringBuilder result3 = new StringBuilder();
            foreach (var p in foundProducts3)
            {
                result3.AppendFormat("Price: {0} ", p.Price);
            }

            // Deferred LINQ
            var foundProducts4 = products.OrderByDescending(e => e.Price)
                .Take(3)
                .Select(e => new
                {
                    e.Name,
                    e.Price
                });

            // Non-deferred LINQ
            var result5 = products.Sum(e => e.Price);

            products[2] = new Product { Name = "Stadium", Price = 79600M };

            StringBuilder result4 = new StringBuilder();
            foreach (var p in foundProducts4)
            {
                result4.AppendFormat("Price: {0} ", p.Price);
            }

            return View("Result", (object)String.Format("{0} --- {1} --- {2} --- {3} --- {4}", result1, result2, result3, result4, result5));
        }