LINQ學習重點(WHERE)

 

Restriction operators: The where keyword

The where keyword or Where method provide this capability. These operators restrict, or filter, the input sequence to produce an output sequence.


//Filter elements based on position(這種寫法,第1次看)
        static void Ex4()
        {
            string[] digits = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
            var shortDigits = digits.Where((digit, index) => digit.Length < index); //  "five", "six", "seven", "eight", "nine" , 這幾個被篩選出來
            WriteLine("==Query Result==");
            foreach (var item in shortDigits)
            {
                WriteLine($" The Word <{item}> 比它的索引值短");
            }
        }

        //Examine a sequence property of output elements
        static void Ex3()
        {
            var customers = GetCustomerList();
            var tpeCustomers = from cus in customers
                               where cus.Region == "TPE"
                               select cus;
            WriteLine("==Query Result==");
            foreach (var item in tpeCustomers)
            {
                WriteLine($" Customer<{item.CustomerID}> : {item.CompanyName}");
                foreach (var order in item.Orders)
                {
                    WriteLine($" OrderID <{order.OrderID}> : {order.OrderDate : yyyy/MM/dd}");
                }
            }
        }

        //Filter elements on multiple properties
        static void Ex2()
        {
            List<Product> products = GetProductList();
            var expensiveProducts = from prod in products
                                    where prod.UnitsInStock > 0 && prod.UnitPrice > 3.00M
                                    select prod;
            WriteLine("==Query Result==");
            foreach (var item in expensiveProducts)
            {
                WriteLine($"{item.ProductName} 在庫:({item.UnitsInStock}) & 金額大於 3.00M:({item.UnitPrice})");
            }
        }

        //Filter elements on a property
        static void Ex1()
        {
            List<Product> products = GetProductList();
            var soldOuts = from prod in products
                           where prod.UnitsInStock == 0
                           select prod;
            WriteLine("Sold out Products : ");
            foreach (var item in soldOuts)
            {
                WriteLine($"{ item.ProductName } is sold Out.");
            }
        }

        static List<Product> GetProductList()
        {
            var list = new List<Product>();

            list.Add(new Product { ProductName = "Cheese", UnitPrice = 3000.58M, UnitsInStock = 20 });
            list.Add(new Product { ProductName = "Apple", UnitPrice = 5M, UnitsInStock = 10 });
            list.Add(new Product { ProductName = "Banana", UnitPrice = 2.55M, UnitsInStock = 0 });
            list.Add(new Product { ProductName = "Milk", UnitPrice = 258M, UnitsInStock = 365 });
            list.Add(new Product { ProductName = "Cherry", UnitPrice = 147M, UnitsInStock = 15 });

            return list;
        }

        static List<Customer> GetCustomerList()
        {
            var list = new List<Customer>();
            list.Add(new Customer()
            {
                CustomerID = "Kevin" ,
                CompanyName ="BOT" ,
                Region ="TPE" ,
                Orders = new List<Order> { new Order {  OrderID="1" , OrderDate = Convert.ToDateTime("2020/11/26") } 
                                                                  , new Order {  OrderID="2" , OrderDate = Convert.ToDateTime("2020/11/27") } }
            });

            list.Add(new Customer()
            {
                CustomerID = "Bob",
                CompanyName = "LAND",
                Region = "TPE",
                Orders = new List<Order> { new Order {  OrderID="6" , OrderDate = Convert.ToDateTime("2020/10/01") }
                                                                  , new Order {  OrderID="7" , OrderDate = Convert.ToDateTime("2020/10/03") } }
            });

            list.Add(new Customer()
            {
                CustomerID = "Judy",
                CompanyName = "TFMI",
                Region = "KAO",
                Orders = new List<Order> { new Order {  OrderID="3" , OrderDate = Convert.ToDateTime("2020/11/03") }
                                                                 , new Order {  OrderID="4" , OrderDate = Convert.ToDateTime("2020/11/15") } }
            });

            return list;
        }

    }

    class Product
    {
        public string ProductName { get; set; }
        public int UnitsInStock { get; set; }
        public decimal UnitPrice { get; set; }
    }

    class Customer
    {
        public string Region { get; set; }
        public string  CustomerID { get; set; }
        public string  CompanyName { get; set; }
        public List<Order> Orders { get; set; }
    }

    class Order
    {
        public string OrderID { get; set; }
        public DateTime OrderDate { get; set; }
    }

留言

這個網誌中的熱門文章

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

載入JavaScript code的最佳時機