Пример #1
0
        public async Task <ViewResult> Index()
        {
            long?length = await MyAsyncMethods.GetPageLength();

            string[] strArr = new string[] { $"Length: {length}", " I'm nice!" };
            return(View(strArr));
        }
Пример #2
0
        public async Task <ViewResult> UsingAsync()
        {
            MyAsyncMethods myAsync = new MyAsyncMethods();
            long           length  = await myAsync?.GetPageLength() ?? 0;

            return(View(length));
        }
Пример #3
0
        public async Task <ViewResult> Index()
        {
            long?length = await MyAsyncMethods.GetPageLength();

            ShoppingCart cart = new ShoppingCart {
                Products = Product.GetProducts()
            };

            decimal priceFilterTotal = cart.Filter(p => (p?.Price ?? 0) >= 20).TotalPrices();

            List <string> results = new List <string>();

            foreach (Product p in Product.GetProducts())
            {
                string  name        = p?.Name ?? "<No Name>";
                decimal?price       = p?.Price ?? 0;
                string  relatedName = p?.Related?.Name ?? "<None>";
                results.Add($"Name: {name}, Price: {price:C2}, Related: {relatedName}");
            }


            results.Add($"Total Cost of items that are more expensive than 20$: {priceFilterTotal}");
            results.Add($"Length: {length}");
            return(View(results));
        }
Пример #4
0
        public async Task <ViewResult> Index18()
        {
            //Page 102, not showing correct ans.
            long?length = await MyAsyncMethods.GetPageLength();

            return(View(new string[] { $"Length: {length}" }));
        }
Пример #5
0
        public async Task <ViewResult> Index()
        {
            List <string> results = new List <string>();

            var products = new Dictionary <string, Product>
            {
                ["Soap"] = new Product(false)
                {
                    Name = "Soap", Price = 22.35m
                },
                ["Shampoo"] = new Product(false)
                {
                    Name = "Shampoo", Price = 78.89m
                }
            };

            Func <Product, bool> nameFilter = delegate(Product product)
            {
                return(product?.Name?[0] == 'S');
            };

            foreach (Product p in Product.GetProducts().Filter(FilterExpensivePrice))
            {
                string  name        = p?.Name ?? "<No Name>";
                decimal?price       = p?.Price ?? 0m;
                string  relatedName = p?.Related?.Name ?? "<None>";

                //results.Add(string.Format("Name: {0}, Price: {1}, Related: {2}",
                //name, price, relatedName));
                results.Add($"{nameof(p.Name)}: {name}, {nameof(p.Price)}: {price:C2}, {nameof(p.Related)}: {relatedName}");
            }

            ShoppingCart cart = new ShoppingCart()
            {
                Products = Product.GetProducts().FilterByPrice(20)
            };

            Order order = new Order()
            {
                Products = Product.GetProducts()
            };

            decimal total = cart.TotalPrice();
            decimal totalOnlyExpensives = order.Filter(FilterExpensivePrice).Total();

            results.Add($"Total: {total:C2}");

            var yahooLenght = await MyAsyncMethods.GetPageLengthOldWay();

            var appleLenght = await MyAsyncMethods.GetPageLengthNewWay();

            results.Add("");
            results.Add($"Length of yahoo page is {yahooLenght} \r\nLength of apple page is {appleLenght}");

            return(View(results));

            //return View(new string[] {"C#", "Language", "Features"});
        }
        // BOOK p103 - using an asynchronous Enumerable
        public async Task <ViewResult> Index_p103()
        {
            List <string> output = new List <string>();

            await foreach (long?len in MyAsyncMethods.GetPageLengths(output, "apress.com", "microsoft.com", "amazon.co.uk"))
            {
                output.Add($"Page length: {len}");
            }
            return(View(output));
        }
Пример #7
0
        public async Task <IActionResult> IndexAsync()
        {
            List <string> output = new List <string>();

            //foreach (long? len in await MyAsyncMethods.GetPageLengths(output, "apress.com", "microsoft.com", "amazon.com"))
            //{
            //    output.Add($"Page length: { len}");
            //}

            await foreach (long?len in MyAsyncMethods.GetPageLengths(output, "apress.com", "microsoft.com", "amazon.com"))
            {
                output.Add($"Page length: { len}");
            }
            return(View());
        }
Пример #8
0
        public async Task <ViewResult> Asynchronous()
        {
            //Asynchronous methods
            long?traditionalLength = await MyAsyncMethods.TraditionalGetPageLength();

            long?simplyLength = await MyAsyncMethods.SimplifyGetPageLength();

            Console.WriteLine($"traditionalLength:{traditionalLength}, simplyLength:{simplyLength}");

            //Asynchronous Enumerable
            List <String> output = new List <string>();

            await foreach (long?length in MyAsyncMethods.GetPageLength(output, "apress.com", "microsoft.com", "amazon.com"))
            {
                output.Add($"Page length : {length}");
            }
            foreach (string item in output)
            {
                Console.WriteLine(item);
            }
            return(View(new string[] { $"traditionalLength:{traditionalLength}, simplyLength:{simplyLength}" }));
        }
        //public ViewResult Index()
        //{
        //    List<string> results = new List<string>();

        //    foreach (Product p in Product.GetProducts())
        //    {   // example of Null Conditional Operator ? combined with Coalescing Operator ??
        //        string name = p?.Name ?? "<none>";
        //        decimal? price = p?.Price ?? 0;
        //        string relatedName = p?.Related?.Name ?? "<none>";
        //        string category = p?.Category;
        //        bool? inStock = p?.InStock;
        //        results.Add($"Name: {name}, Price: {price}, Related: {relatedName}, Category: {category}, In Stock: {inStock}");
        //    }

        //    return View(results);
        //}
        //using the extention method Twice on a string object
        //public ViewResult Index()
        //{
        //    string name = "Bob";
        //    var extMeth = name.Twice();
        //    return View("Index", new string[] {extMeth, extMeth});
        //}


//        public ViewResult Index()
//        {
//            var querySample = from item in Product.AnonPoductList select item.Name;
//            return View(querySample);
//        }

        /*ASYNC METHODS*/
//        public async Task<long?> Index()
//        {
//            long? length=  await MyAsyncMethods.GetPageLength();
//            return length;
//        }
//        public async Task<string> Index()
//        {
//            string content=  await MyAsyncMethods.GetPageAsString();
//            return content;
//        }

        public IActionResult Index()
        {
            var content = MyAsyncMethods.GetPageAsString().Result["results"];

            return(View(content));
        }
Пример #10
0
        public async Task <IActionResult> Index3()
        {
            long?length = await MyAsyncMethods.GetPageLength();

            return(View("Index", new string[] { $"Length: {length}" }));
        }
Пример #11
0
 public Task <long?> TaskAction()
 {
     return(MyAsyncMethods.GetPageLengthAsync());
 }
Пример #12
0
        public ViewResult GetAsyncResult()
        {
            var contentLength = MyAsyncMethods.GetPageLength2();

            return(View("Results", (object)String.Format("Content Length of Apress.com is: {0}", contentLength.ToString())));
        }
Пример #13
0
        public async Task <long?> Index()
        {
            long?contentLength = await MyAsyncMethods.GetPageLength();

            return(contentLength);
        }
        public async Task <IActionResult> PageLength()
        {
            ViewBag.PageLength = await MyAsyncMethods.GetPageLength();

            return(View());
        }
Пример #15
0
        public async Task <ViewResult> Index()
        {
            long?length = await MyAsyncMethods.GetPageLength();

            List <string> results = new List <string>();

            /*
             * Null-Condition-Operator:
             * Kann nur auf Properties die Null sein können angewendet werden.
             * Der Operator verhindert das Null Properties verarbeitet werden und
             * damit eine NullReferenceException ausgelöst wird.
             * Coalescing-Operrator (Coalescing = Verschmelzung):
             * Mit dem ?? kann ein Fallbackwert implementiert werden.
             */
            foreach (Product p in Product.GetProducts())
            {
                string  name        = p?.Name ?? "<No Name>";
                decimal?price       = p?.Price ?? 0;
                string  relatedName = p?.Related?.Name ?? "<None>";
                // Stringaufbau mit Formatmethode
                //results.Add(string.Format("Name: {0}, Price: {1}, Related: {2}", name, price, relatedName));
                // String-Interpolation: Direktes Einbinden von Variablennamen in ein String.
                results.Add($"Name: {name}, Price: {price}, Related: {relatedName}");
            }

            Product[] productArray =
            {
                new Product()
                {
                    Name = "Kayak", Price = 275M
                },
                new Product()
                {
                    Name = "Lifejacket", Price = 48.95M
                }
            };

            Product[] yieldArray =
            {
                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
                }
            };



            ShoppingCart cart = new ShoppingCart()
            {
                Products = Product.GetProducts()
            };
            decimal cartTotal  = cart.TotalPrices();
            decimal arrayTotal = productArray.TotalPrices();
            decimal yieldTotal = yieldArray.FilterByPrice(20).TotalPrices();

            results.Add(string.Format($"Total: {cartTotal:C2} TotalArray: {arrayTotal:C2} YieldArrayTotal: {yieldTotal:C2}"));
            results.Add($"Length: {length}");

            return(View(results));
        }
Пример #16
0
 public ViewResult GetLength2()
 {
     return(View("Result", (object)MyAsyncMethods.GetPageLength2().ToString()));
 }
        public async Task <ViewResult> Index()
        {
            #region product
            // List<string> results = new List<string>();
            #region first
            //  foreach (Product p in Product.GetProducts())
            //  {
            //    string name = p?.Name ?? "<No Name>";
            //    decimal? price = p?.Price ?? 0.00m;
            //    string relatedName = p?.Related?.Name ?? "<None>";
            //    results.Add(string.Format("Name: {0}, Price: {1:C2}, Related: {2}",
            //     name, price, relatedName));
            //}
            //return View(results);
            // }
            ///////// second
            //Dictionary<string, Product> products = new Dictionary<string, Product>
            //{
            //    ["Kayak"] = new Product { Name = "Kayak", Price = 275M },
            //    ["Lifejacket"] = new Product { Name = "Lifejacket", Price = 48.95M }
            //};
            //return View("Index", products.Keys);
            #endregion

            #region using keyword is
            object[] data = new object[] { 275M, 29.95M,
                                           "apple", "orange", 100, 10 };
            decimal total = 0;
            /////////// using keyword is

            //            for (int i = 0; i < data.Length; i++)
            //            {
            //                if (data[i] is decimal d)
            //                {
            //                    total += d;
            //                }
            //            }
            //            return View("Index", new string[] { $"Total: {total:C2}" });
            #endregion

            #region using switch when
            //for (int i = 0; i < data.Length; i++)
            //{
            //    switch (data[i])
            //    {
            //        case decimal decimalValue:
            //            total += decimalValue;
            //            break;
            //        case int intValue when intValue > 50:
            //            total += intValue;
            //            break;
            //    }
            //}
            //return View("Index", new string[] { $"Total: {total:C2}" });
            #endregion

            #region using Extencion
            //            ShoppingCart cart
            //= new ShoppingCart { Products = Product.GetProducts() };
            //            decimal cartTotal = cart.TotalPrices();
            //            return View("Index", new string[] { $"Total: {cartTotal:C2}" });
            //        }
            #endregion

            #region using extencion for Interface

            //ShoppingCart cart
            //= new ShoppingCart { Products = Product.GetProducts() };
            //Product[] productArray = {
            //new Product {Name = "Kayak", Price = 275M},
            //new Product {Name = "Lifejacket", Price = 48.95M}
            //  };
            //decimal cartTotal = cart.TotalPrices();
            //decimal arrayTotal = productArray.TotalPrices();
            //return View("Index", new string[] {
            //   $"Cart Total: {cartTotal:C2}", $"Array Total: {arrayTotal:C2}" });

            #endregion

            #region using extencion for interface by filter
            //Product[] productArray = {
            //     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 arrayTotal = productArray.FilterByPrice(20).TotalPrices();
            //return View("Index", new string[] { $"Array Total: {arrayTotal:C2}" });
            #endregion

            #region using extencion and lyamda expression

            //           Product[] productArray = {
            //               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}
            //               };
            //           Func<Product, bool> nameFilter = delegate (Product prod) {
            //               return prod?.Name?[0] == 'S';
            //           };
            //           decimal priceFilterTotal = productArray
            //           .Filter(p => (p?.Price ?? 0) >= 20)
            //           .TotalPrices();
            //           decimal nameFilterTotal = productArray
            //           .Filter(p => p?.Name?[0] == 'S')
            //           .TotalPrices();
            //           return View("Index", new string[] {
            //$"Price Total: {priceFilterTotal:C2}",
            //$"Name Total: {nameFilterTotal:C2}" });
            //       }
            //       bool FilterByPrice(Product p)
            //       {
            //           return (p?.Price ?? 0) >= 20;
            //       }
            #endregion

            #region using anonymus type
            //           var products = new[] {
            //new { Name = "Kayak", Price = 275M },
            //new { Name = "Lifejacket", Price = 48.95M },
            //new { Name = "Soccer ball", Price = 19.50M },
            //new { Name = "Corner flag", Price = 34.95M }
            //};
            //           return View(products.Select(p => p.GetType().Name));
            #endregion

            #endregion

            long?length = await MyAsyncMethods.GetPageLength();

            return(View(new string[] { $"Length: {length}" }));
        }
        public async System.Threading.Tasks.Task <ViewResult> ShowLength2()
        {
            long?len = await MyAsyncMethods.GetPageLength2();

            return(View("Result", (object)String.Format("page length: {0}", len)));
        }
Пример #19
0
        // bool FilterByPrice(Product p)
        // {
        //     return (p?.Price ?? 0) >= 20;
        // }
        public async Task <ViewResult> Index()
        {
            #region
            // //The null conditional operator allows for null values to be detected more elegantly(thanh lich) :).
            // List<string> results = new List<string>();
            // foreach (Product p in Product.GetProducts())
            // {
            //     // string name = p?.Name;
            //     // decimal? price = p?.Price;
            //     // string relatedName = p?.Related?.Name;

            //     //Combining the Conditional and Coalescing Operators
            //     string name = p?.Name ?? "<No Name>";
            //     decimal? price = p?.Price ?? 0;
            //     string relatedName = p?.Related?.Name ?? "<None>";

            //     // results.Add(string.Format("Name: {0}, Price: {1}", name, price));
            //     results.Add(string.Format("Name: {0}, Price: {1}, Related: {2}", name, price, relatedName));
            // }
            // return View(results);
            // // return View(new string[] { "C#", "Language", "Features" });

            //----------------------------------------------------------------
            // Using Object and Collection Initializers
            // string[] names = new string[3];
            // names[0] = "Bob";
            // names[1] = "Joe";
            // names[2] = "Alice";
            // return View("Index", names);

            // return View("Index", new string[] { "Bob", "Joe", "Alice" });
            //--------------------------------
            // Using an Index Initializer
            // Dictionary<string, Product> products = new Dictionary<string, Product> {
            //     { "Kayak", new Product { Name = "Kayak", Price = 275M } }
            //   , { "Lifejacket", new Product{ Name = "Lifejacket", Price = 48.95M } }
            // };
            // return View("Index", products.Keys);

            // Dictionary<string, Product> products = new Dictionary<string, Product> {
            //     ["Kayak"] = new Product { Name = "Kayak", Price = 275M }
            //   , ["Lifejacket"] = new Product { Name = "Lifejacket", Price = 48.95M }
            // };
            // return View("Index", products.Keys);

            //Pattern matching
            // object[] data = new object[] { 275M, 29.95M, "apple", "orange", 100, 10 };
            // decimal total = 0;
            // for (int i = 0; i < data.Length; i++)
            // {
            //     if (data[i] is decimal d)
            //     {
            //         total += d;
            //     }
            // }

            // return View("Index", new string[] { $"Total: {total:C2}" });

            //Pattern Matching in switch Statements
            // for (int i = 0; i < data.Length; i++) {
            //     switch (data[i]) {
            //         case decimal decimalValue:
            //             total += decimalValue;
            //             break;
            //         case int intValue when intValue > 50:
            //             total += intValue;
            //             break;
            //     }
            // }
            // return View("Index", new string[] { $"Total: {total:C2}" });
            // ShoppingCart cart = new ShoppingCart { Products = Product.GetProducts() };

            // Product[] productArray = { new Product {Name = "Kayak", Price = 275M},
            //                            new Product {Name = "Lifejacket", Price = 48.95M} };
            // decimal cartTotal = cart.TotalPrices();
            // decimal arrayTotal = productArray.TotalPrices();
            // return View("Index", new string[] {
            //     $"Cart Total: {cartTotal:C2}",
            //     $"Array Total: {arrayTotal:C2}" });
            // Product[] productArray = {
            //         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 arrayTotal = productArray.FilterByPrice(20).TotalPrices();
            // decimal priceFilterTotal = productArray.FilterByPrice(20).TotalPrices();
            // decimal nameFilterTotal = productArray.FilterByName('S').TotalPrices();

            // // return View("Index", new string[] { $"Array Total: {arrayTotal:C2}" });
            // return View("Index", new string[] {
            //       $"Price Total: {priceFilterTotal:C2}"
            //     , $"Name Total: {nameFilterTotal:C2}" });
            // / khoong cos cachs tiep can naof la lys tuongw
            // Func<Product, bool> nameFilter = delegate (Product prod)
            // {
            //     return prod?.Name?[0] == 'S';
            // };
            // decimal priceFilterTotal = productArray.Filter(FilterByPrice).TotalPrices();
            // decimal nameFilterTotal = productArray.Filter(nameFilter).TotalPr
            // decimal priceFilterTotal = productArray.Filter(p => (p?.Price ?? 0) >= 20).TotalPrices();
            // decimal nameFilterTotal = productArray.Filter(p => p?.Name?[0] == 'S').TotalPrices();
            // return View("Index", new string[] {$"Price TOTAL: {priceFilterTotal:C2}",
            //                                     $"Name Total: {nameFilterTotal:C2}"});


            // return View(Product.GetProducts().Select(p=> p?.Name));

            //Using anonymous type
            // var names = new [] { "Kayak", "Lifejacket", "Soccer ball"};
            // return View(names);
            // var products = new[] {
            //         new { Name = "Kayak", Price = 275M },
            //         new { Name = "Lifejacket", Price = 48.95M },
            //         new { Name = "Soccer ball", Price = 19.50M },
            //         new { Name = "Corner flag", Price = 34.95M }
            //         };
            // return View(products.Select(p => p.Name));
            // return View(products.Select(p => p.GetType().Name));
            //Using Default Implementations in Interfaces
            // IProductSelection cart = new ShoppingCart(
            //     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 });
            // // return View(cart.Products.Select(p => p.Name));
            // return View(cart.Names);
            #endregion
            //Using Asynchronous Methods
            // long? length = await MyAsyncMethods.GetPageLength();
            // return View(new string[] { $"Length: {length}"
            // });
            List <string> output = new List <string>();
            await foreach (long?len in MyAsyncMethods.GetPageLengths(output, "apress.com", "microsoft.com", "amazon.com"))
            {
                output.Add($"Page length: { len}");
            }
            return(View(output));
        }
Пример #20
0
        public async Task <ViewResult> AsyncAction()
        {
            var contentLength = await MyAsyncMethods.GetPageLengthAsync();

            return(View("Result", (object)String.Format("Page Length: {0}", contentLength)));
        }
        public ViewResult ShowLength()
        {
            long?len = MyAsyncMethods.GetPageLength().Result;

            return(View("Result", (object)String.Format("page length: {0}", len)));
        }
Пример #22
0
        public async Task <ViewResult> Index()
        {
            //return View(new string[] { "C#", "Language", "Features" });
            List <string> results = new List <string>();

            foreach (Product p in Product.GetProducts())
            {
                string  name        = p?.Name ?? "<No Name>";
                decimal?price       = p?.Price ?? 0;
                string  relatedName = p?.Related?.Name ?? "<None>";
                //results.Add(string.Format("Name: {0}, Price:{1}, Related:{2} ", name, price,relatedName));
                results.Add($"Name:{name}, Price:{price}, Related:{relatedName}");
            }
            Dictionary <string, Product> products = new Dictionary <string, Product>
            {
                ["Kayak"] = new Product {
                    Name = "Kayak", Price = 275M
                },
                ["Lifejacket"] = new Product {
                    Name = "Lifejacket", Price = 48.95M
                }
            };

            //return View(results);
            //return View("Index", products.Keys);
            object[] data  = new object[] { 275M, 29.95M, "apple", "orange", 100, 10 };
            decimal  total = 0;

            //for (int i = 0; i < data.Length; i++)
            //{
            //    if(data[i] is decimal d)
            //    {
            //        total += d;
            //    }
            //}
            for (int i = 0; i < data.Length; i++)
            {
                switch (data[i])
                {
                case decimal d:
                    total += d;
                    break;

                case int intValue when intValue > 50:
                    total += intValue;
                    break;

                default:
                    break;
                }
            }
            ShoppingCart cart = new ShoppingCart {
                Products = Product.GetProducts()
            };

            Product[] prodArray =
            {
                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();
            decimal arrayTotal = prodArray.FilterByPrice(20).TotalPrices();
            //return View("Index", new string[] { $"Cart total:{cartTotal:C2}", $"Array total:{arrayTotal:C2}" });
            Func <Product, bool> nameFilter = delegate(Product prod)
            {
                return(prod?.Name[0] == 'S');
            };
            //decimal nameFilterTotal = prodArray.Filter(nameFilter).TotalPrices();
            //decimal priceFilterTotal = prodArray.Filter(FilterByPrice).TotalPrices();

            decimal priceFilterTotal = prodArray.Filter(p => (p?.Price ?? 0) >= 20).TotalPrices();
            decimal nameFilterTotal  = prodArray.Filter(p => (p?.Name[0] == 'S')).TotalPrices();

            //return View("Index", new string[] { $"Price total:{priceFilterTotal:C2}", $"Name total:{nameFilterTotal}" });
            //return View(Product.GetProducts().Select(p => p?.Name));

            long?length = await MyAsyncMethods.GetPageLength1();

            string content = await MyAsyncMethods.GetPageContent();

            return(View(new string[] { $"Length:{length}", $"Content:{content}" }));
        }
Пример #23
0
 public ViewResult FindLenghtAsync()
 {
     return(View("Result", (object)MyAsyncMethods.GetPgLength()));
 }
Пример #24
0
        public async Task <ViewResult> ContentLength()
        {
            long?length = await MyAsyncMethods.GetPathLengthAsync();

            return(View("Index", new [] { $"Length: {length}" }));
        }
Пример #25
0
        /* Using string interpolation
         * public ViewResult Index()
         * {
         *
         *  List<string> results = new List<string>();
         *
         *  foreach(Product p in Product.GetProducts())
         *  {
         *      string name = p?.Name ?? "<NoName>";
         *      decimal? price = p?.Price ?? 0;
         *      string relatedName = p?.Related?.Name ?? "<None>";
         *      bool available = p?.InStock ?? false;
         *      results.Add($"Name: {name}, Price: {price}, Related: {relatedName}, Available: {available}");
         *      // better than use the string.Format(...{0}, name)... syntax
         *
         *  }
         *  return View(results);
         *
         * }*/

        /*
         * public ViewResult Index()
         * {
         *
         *  //It is admitted to initialize the model on the View call
         *  return View("Index", new string[] { "Hola", "Eho", "huhuh" });
         *
         * }*/
        /*
         * public ViewResult Index()
         * {
         *  Dictionary<string, Product> products = new Dictionary<string, Product> {
         *          { "Kayak", new Product { Name = "Kayak", Price = 275M } },
         *          { "Lifejacket", new Product{ Name = "Lifejacket", Price = 48.95M } }
         *      };
         *  // Can be also declared as this, using Index initializers, avoiding the excess of {{}}
         *  Dictionary<string, Product> productsSecondInitialiation = new Dictionary<string, Product>
         *  {
         *      ["kayak"] = new Product { Name = "Kayak", Price = 275M },
         *      ["Lifejacket"] = new Product { Name = "Lifejacket", Price = 48.95M }
         *  }
         *
         *
         *  return View("Index", products.Keys);
         *
         * }*/

        //Pattern Matching example

        /* public ViewResult Index()
         * {
         *   object[] data = new object[] { 275M, 29.95M, "apple", "orange", 100, 10 };
         *   decimal total = 0;
         *
         *   for(int i = 0; i<data.Length; i++)
         *   {
         *       if(data[i] is decimal d)
         *       {
         *           total += d;
         *       }
         *
         *   }
         *   return View("Index", new string[] { $"Total: {total:C2}" });
         * }*/

        //Pattern matching in Switch statements.

        /*
         * public ViewResult Index()
         * {
         *  object[] data = new object[] { 275M, 29.95M, "apple", "orange", 100, 10 };
         *  decimal total = 0;
         *
         *  for (int i = 0; i < data.Length; i++)
         *  {
         *      switch (data[i])
         *      {
         *          case decimal decimalValue:
         *              total += decimalValue;
         *              break;
         *          case int intValue when intValue > 50:
         *              total += intValue;
         *              break;
         *      }
         *
         *  }
         *  return View("Index", new string[] { $"Total: {total:C2}" });
         * }*/

        //Extension methods
        //The method TotalPrices() defined on Extension Methods class can be used directly over ShoppingCart instances

        /*
         * public ViewResult Index()
         * {
         * ShoppingCart cart
         * = new ShoppingCart { Products = Product.GetProducts() };
         * decimal cartTotal = cart.TotalPrices();
         * return View("Index", new string[] { $"Total: {cartTotal:C2}" });
         * }*/

        // Using the extension method on an Interface.

        /// Type inference

        /* public ViewResult Index()
         * {
         *   var products = new[] {
         *                           new { Name = "Kayak", Price = 275M },
         *                           new { Name = "Lifejacket", Price = 48.95M },
         *                           new { Name = "Soccer ball", Price = 19.50M },
         *                           new { Name = "Corner flag", Price = 34.95M }
         *                        };
         *   return View(products.Select(p => p.GetType().Name));
         * }
         */

        public async Task <ViewResult> Index()
        {
            long?length = await MyAsyncMethods.GetPageLength();

            return(View(new string[] { $"Length: {length}" }));
        }
Пример #26
0
        //public ViewResult Index()
        //{
        ////*****pg 103-104
        //var products = new[]
        //{
        //    new { Name = "Kayak", Price = 275M },
        //    new { Name = "Lifejacket", Price = 48.95M },
        //    new { Name = "Soccer Ball", Price = 19.50M },
        //    new { Name = "Corner flag", Price = 34.95M }
        //};
        //return View(products.Select(p => $"{nameof(p.Name)}: {p.Name}, {nameof(p.Price)}: {p.Price}"));

        //*****pg 102
        public async Task <ViewResult> Index()
        {
            long?length = await MyAsyncMethods.GetPageLength();

            return(View(new string[] { $"Length: {length}" }));

            //public ViewResult Index()
            //{
            //    //*****pg 97-98 part 2
            //    var products = new[]
            //    {
            //        new { Name = "Kayak", Price = 275M },
            //        new { Name = "Lifejacket", Price = 48.95M },
            //        new { Name = "Soccer Ball", Price = 19.50M },
            //        new { Name = "Corner flag", Price = 34.95M }
            //    };
            //    return View(products.Select(p => p.GetType().Name));

            ////*****pg 97
            //var names = new[] { "Kayak", "Lifejacket", "Soccer Ball" };
            //return View(names);


            ////*****pg 92
            //bool FilterByPrice(Product p)
            //{
            //    return (p?.Price ?? 0) >= 20;
            //}
            ////*****pg 95 part 2
            //public ViewResult Index() => View(Product.GetProducts().Select(p => p?.Name));

            //public ViewResult Index()
            //{
            ////*****pg 95
            //return View(Product.GetProducts().Select(p => p?.Name));

            ////*****pg 93

            //Product[] productArray =
            //{
            //    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 }
            //};

            //Func<Product, bool> nameFilter = delegate (Product prod)
            //{
            //    return prod?.Name?[0] == 'S';
            //};

            //decimal priceFilterTotal = productArray.Filter(p => (p?.Price ?? 0) >= 20).TotalPrices();
            //decimal nameFilterTotal = productArray.Filter(p => p?.Name?[0] == 'S').TotalPrices();

            ////decimal priceFilterTotal = productArray.FilterByPrice(20).TotalPrices();
            ////decimal nameFilterTotal = productArray.FilterByName('S').TotalPrices();

            //return View("Index", new string[]
            //{
            //    //$"Total: {cartTotal:c2}",
            //    $"Price Total: {priceFilterTotal:c2}",
            //    $"Name Total: {nameFilterTotal:c2}"
            //});

            ////*****pg 89
            //ShoppingCart cart = new ShoppingCart { Products = Product.GetProducts() };

            //Product[] productArray =
            //{
            //    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();
            //decimal arrayTotal = productArray.FilterByPrice(20).TotalPrices();


            //return View("Index", new string[]
            //{
            //    //$"Total: {cartTotal:c2}",
            //    $"Array Total: {arrayTotal:c2}"
            //});

            ////*****pg 86
            //ShoppingCart cart = new ShoppingCart { Products = Product.GetProducts() };
            //decimal cartTotal = cart.TotalPrices();
            //return View("Index", new string[] { $"Total: {cartTotal:c2}" });

            ////*****pg 84 -85
            //object[] data = new object[]
            //{
            //    275M, 29.95M, "apple", "orange", 100, 10
            //};
            //decimal total = 0;
            //for (int i = 0; i < data.Length; i++)
            //{
            //    switch (data[i])
            //    {
            //        case decimal decimalValue:
            //            total += decimalValue;
            //            break;
            //        case int intValue when intValue > 50:
            //            total += intValue;
            //            break;
            //    }
            //    //if (data[i] is decimal d)
            //    //{
            //    //    total += d;
            //    //}
            //}
            //return View("Index", new string[] { $"Total: {total:c2}" });

            ////*****pg 83
            //Dictionary<string, Product> products = new Dictionary<string, Product>
            //{
            //    ["Kayak"] = new Product { Name = "Kayak", Price = 275M },
            //    ["Lifejacket"] = new Product{ Name = "Lifejacket", Price = 48.95M }
            //};
            //return View("Index", products.Keys);

            ////*****pg 82
            //Dictionary<string, Product> products = new Dictionary<string, Product>
            //{
            //    { "Kayak", new Product { Name = "Kayak", Price = 275M } },
            //    { "Lifejacket", new Product{ Name = "Lifejacket", Price = 48.95M } }
            //};
            //return View("Index", products.Keys);

            ////*****pg 81
            //return View("Index", new string[] { "Bob", "Joe", "Alice" });


            ////*****pg 67 - 80
            //List<string> results = new List<string>();

            //foreach (Product p in Product.GetProducts())
            //{

            //    string name = p?.Name ?? "<No Name>";
            //    decimal? price = p?.Price ?? 0;
            //    string relatedName = p?.Related?.Name ?? "<None>";

            //    results.Add($"Name: {name}, Price: {price}, Related: {relatedName}");
            //}
            //return View(results);
        }