LINQ學習重點(SELECT 1)

 //Select a subset of properties

        static void Ex7()

        {

            List<Product> products = GetProductList();

            var productInfos = from p in products

                               select new { p.ProductName , Price = p.UnitPrice};

            foreach (var item in productInfos)

            {

                Console.WriteLine($"{item.ProductName} is and costs {item.Price} per unit.");

            }

        }


        static void Ex7_Tuple() //元組寫法

        {

            List<Product> products = GetProductList();

            //var productInfos = from p in products

            //                   select (p.ProductName , Price : p.UnitPrice);

            //foreach (var item in productInfos)

            //{

            //    Console.WriteLine($"{item.ProductName} is and costs {item.Price} per unit.");

            //}

        }


        //Use select to create new types

        static void Ex6()

        {

            int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };

            string[] strings = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };


            var digitOddEvens = from n in numbers

                                select new {Digit = strings[n] , Even = (n % 2 ==0) };

            foreach (var item in digitOddEvens)

            {

                WriteLine($"The digit  {item.Digit} is  {(item.Even ? "even" : "odd")} ");

            }

        }


        static void Ex6_Tuple() //ValueTuple的寫法

        {

            int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };

            string[] strings = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };

            //var digitOddEvens = from n in numbers

            //                    select (Digit : strings[n] , Even: (n % 2 == 0));

            //foreach (var item in digitOddEvens)

            //{

            //    WriteLine($"The digit  {item.Digit} is  {(item.Even ? "even" : "odd")} ");

            //}

        }


        //Select anonymous types(匿名型別) , 範例Ex5 跟 Ex6 功能相同,差別在於 一個用(匿名型別) , 另一個用(元組型別) , 兩種是互補的

        static void Ex5()

        {

            string[] words = { "Apple", "Banana", "ChErry" };

            var upperLowerWords = from w in words

                                  select new { Upper = w.ToUpper(), Lower = w.ToLower() };

            foreach (var ul in upperLowerWords)

            {

                WriteLine($"Upper : {ul.Upper} , Lower : {ul.Lower}");

            }

        }


        //Select tuples(元組) <== C# 6 不支援

        static void Ex5_Tuple()

        {

            //string[] words = { "aPPLE", "BlUeBeRrY", "cHeRry" };


            //var upperLowerWords = from w in words

            //                      select (Upper: w.ToUpper(), Lower: w.ToLower());


            //foreach (var ul in upperLowerWords)

            //{

            //    Console.WriteLine($"Uppercase: {ul.Upper}, Lowercase: {ul.Lower}");

            //}

        }

留言

這個網誌中的熱門文章

溪和食品有限公司 - 觀光工廠接待人員(薪約30K.免費供餐),今天投遞此份工作。

載入JavaScript code的最佳時機