예제 #1
0
        private static void FirstSimple()
        {
            Console.WriteLine("This sample uses First to return the first matching element as a Product, instead of as a sequence containing a Product.");

            List <Product> products = factory.GetProductList();

            Product productInfo = (from product in products
                                   where product.ProductID == 12
                                   select product).First();

            Console.WriteLine("ProductID = {0}, ProductName = {1} Price = {2}.", productInfo.ProductID, productInfo.ProductName, productInfo.UnitPrice);
        }
예제 #2
0
        private static void OrderBySimpleIII()
        {
            Console.WriteLine("This sample uses orderby to sort a list of products by name.");

            List <Product> products       = factory.GetProductList();
            var            sortedProducts = from product in products
                                            orderby product.ProductName
                                            select new { product.ProductID, product.ProductName, product.UnitPrice };

            foreach (var productInfo in sortedProducts)
            {
                Console.WriteLine("{0} is in the category {1} and costs {2} per unit.", productInfo.ProductID, productInfo.ProductName, productInfo.UnitPrice);
            }
        }
예제 #3
0
        private static void SelectSimple2()
        {
            Console.WriteLine("This sample uses select to return a sequence of just the names of a list of products.");
            List <Product> products = factory.GetProductList();

            var productNames = from product in products
                               select product.ProductName;

            Console.WriteLine("Product Names:");
            foreach (var productName in productNames)
            {
                Console.WriteLine(productName);
            }
        }
예제 #4
0
        private static void DistinctII()
        {
            Console.WriteLine("This sample uses Distinct to find the unique Category names.");

            List <Product> products = factory.GetProductList();

            var categoryNames = (from product in products
                                 select product.Category).Distinct();

            Console.WriteLine("Category names:");
            foreach (var n in categoryNames)
            {
                Console.WriteLine(n);
            }
        }
예제 #5
0
        public static void WhereSimple2()
        {
            Console.WriteLine("This sample uses where to find all products that are out of stock.");

            var productList = factory.GetProductList();

            var soldOutProducts = from product in productList
                                  where product.UnitsInStock == 0
                                  select product;

            Console.WriteLine("Sold out products are");

            foreach (Product item in soldOutProducts)
            {
                Console.WriteLine(item.ProductName);
            }
        }
예제 #6
0
        private static void CrossJoin()
        {
            Console.WriteLine("This sample shows how to efficiently join elements of two sequences based on equality between key expressions over the two.");

            string[] categories = new string[] { "Beverages",
                                                 "Condiments",
                                                 "Vegetables",
                                                 "Dairy Products",
                                                 "Seafood" };

            List <Product> products = factory.GetProductList();

            var crossJoin = from category in categories
                            join product in products on category equals product.Category
                            select new { Category = category, product.ProductName };

            foreach (var productCategory in crossJoin)
            {
                Console.WriteLine(productCategory.ProductName + ": " + productCategory.Category);
            }
        }
예제 #7
0
        private static void AnyGrouped()
        {
            Console.WriteLine("This sample uses Any to return a grouped a list of products only for categories that have at least one product that is out of stock.");

            List <Product> products      = factory.GetProductList();
            var            productGroups = from product in products
                                           group product by product.Category into productGroup
                                           where productGroup.Any(pg => pg.UnitsInStock <= 0)
                                           select new
            {
                Category = productGroup.Key,
                Products = productGroup
            };

            foreach (var item in productGroups)
            {
                Console.WriteLine("Categorqy = {0}, Products : ", item.Category);

                foreach (var productInfo in item.Products)
                {
                    Console.WriteLine("{0} is in the category {1} and costs {2} per unit.", productInfo.ProductID, productInfo.ProductName, productInfo.UnitPrice);
                }
            }
        }
예제 #8
0
        private static void CountGrouped()
        {
            Console.WriteLine("This sample uses Count to return a list of categories and how many products each has.");

            List <Product> products = factory.GetProductList();

            var categoryCounts = from product in products
                                 group product by product.Category into productGroup
                                 select new { Category = productGroup.Key, ProductCount = productGroup.Count() };

            foreach (var item in categoryCounts)
            {
                Console.WriteLine("Category  = {0}, Product Count = {1}", item.Category, item.ProductCount);
            }
        }
예제 #9
0
        private static void GroupBySimpleIII()
        {
            Console.WriteLine("This sample uses group by to partition a list of products by category.");

            List <Product> products = factory.GetProductList();

            var orderGroups = from product in products
                              group product by product.Category into productGroup
                              select new { Category = productGroup.Key, Products = productGroup };

            foreach (var item in orderGroups)
            {
                Console.WriteLine("Category = {0}, Products : ", item.Category);

                foreach (var productInfo in item.Products)
                {
                    Console.WriteLine("{0} is in the category {1} and costs {2} per unit.", productInfo.ProductID, productInfo.ProductName, productInfo.UnitPrice);
                }
            }
        }
예제 #10
0
        private static void ConcatII()
        {
            Console.WriteLine("This sample uses Concat to create one sequence that contains the names of all customers and products, including any duplicates.");

            List <Customer> customers = factory.GetCustomerList();
            List <Product>  products  = factory.GetProductList();

            var customerNames = from customer in customers
                                select customer.CompanyName;

            var productNames = from product in products
                               select product.ProductName;

            var allNames = customerNames.Concat(productNames);

            Console.WriteLine("Customer and product names:");
            foreach (var n in allNames)
            {
                Console.WriteLine(n);
            }
        }