LINQ 并且过滤

class Program
    {
        private List productList;
        public static void Main()
        {
            Program pro = new Program();
            List products = pro.GetProductList();
            var expensiveInStockProducts =
                from prod in products
                where prod.UnitsInStock > 0 && prod.UnitPrice > 20.00M
                select prod;

            Console.WriteLine("库存的产品成本大于20.00:");
            foreach (var product in expensiveInStockProducts)
            {
                Console.WriteLine("{0} 存货的成本单价超过20.00.", product.ProductName);
            }
            Console.ReadLine();
        }
        public  List GetProductList()
        {
            if (productList == null)
                createLists();

            return productList;
        }
        private void createLists()
        {
            productList =
                new List {
                    new Product { ProductID = 1, ProductName = "印度奶茶", Category = "饮料", UnitPrice = 18.0000M, UnitsInStock = 39 },
                    new Product { ProductID = 2, ProductName = "青稞酒", Category = "饮料", UnitPrice = 19.0000M, UnitsInStock = 17 },
                    new Product { ProductID = 3, ProductName = "八角糖浆", Category = "调味品", UnitPrice = 10.0000M, UnitsInStock = 13 },
                    new Product { ProductID = 4, ProductName = "厨师安东法人后裔的调味料", Category = "调味品", UnitPrice = 22.0000M, UnitsInStock = 53 },
                    new Product { ProductID = 5, ProductName = "厨师安东的秋葵", Category = "调味品", UnitPrice = 21.3500M, UnitsInStock = 0 }
                };
        }
    }
    class Product
    {
        /// 
        /// 产品ID
        /// 
        public int ProductID { get; set; }
        /// 
        /// 产品名称
        /// 
        public string ProductName { get; set; }
        /// 
        /// 种类
        /// 
        public string Category { get; set; }
        /// 
        /// 单价
        /// 
        public decimal UnitPrice { get; set; }
        /// 
        /// 库存
        /// 
        public int UnitsInStock { get; set; }
    }


 

你可能感兴趣的:(LINQ 并且过滤)