Beispiel #1
0
        public async Task <ViewResult> Index18()
        {
            //Page 102, not showing correct ans.
            long?length = await MyAsyncMethods.GetPageLength();

            return(View(new string[] { $"Length: {length}" }));
        }
Beispiel #2
0
        public async Task <ViewResult> UsingAsync()
        {
            MyAsyncMethods myAsync = new MyAsyncMethods();
            long           length  = await myAsync?.GetPageLength() ?? 0;

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

            string[] strArr = new string[] { $"Length: {length}", " I'm nice!" };
            return(View(strArr));
        }
Beispiel #4
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));
        }
Beispiel #5
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}" }));
        }
Beispiel #6
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}" }));
        }
        public ViewResult ShowLength()
        {
            long?len = MyAsyncMethods.GetPageLength().Result;

            return(View("Result", (object)String.Format("page length: {0}", len)));
        }
        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}" }));
        }
Beispiel #9
0
        public async Task <IActionResult> Index3()
        {
            long?length = await MyAsyncMethods.GetPageLength();

            return(View("Index", new string[] { $"Length: {length}" }));
        }
Beispiel #10
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());
        }
Beispiel #12
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));
        }
Beispiel #13
0
 public ViewResult GetLength()
 {
     return(View("Result", (object)MyAsyncMethods.GetPageLength().ToString()));
 }
Beispiel #14
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);
        }