예제 #1
0
        public static void DoProc()
        {
            var products = new List <Product4>();
            var productA = new Product4()
            {
                Description = "aaa", Price = 100
            };
            var productB = new Product4()
            {
                Description = "bbb", Price = 200
            };
            var productC = new Product4()
            {
                Description = "ccc", Price = 300
            };

            products.Add(productA);
            products.Add(productB);
            products.Add(productC);

            string[] popularProductNames = { "aaa", "bbb" };
            var      popularProducts     = from p in products
                                           join n in popularProductNames
                                           on p.Description equals n
                                           select p;

            // like as inner join, ccc is not extract.
            foreach (var popularProduct in popularProducts)
            {
                Console.WriteLine(popularProduct.Description);
            }
        }
예제 #2
0
        public static void DoProc()
        {
            var productA = new Product4()
            {
                Description = "aaa", Price = 100
            };
            var productB = new Product4()
            {
                Description = "bbb", Price = 200
            };
            var productC = new Product4()
            {
                Description = "ccc", Price = 300
            };

            var orders = new List <Order4>()
            {
                new Order4()
                {
                    OrderLine = new List <OrderLine4>()
                    {
                        new OrderLine4()
                        {
                            Amount  = 10,
                            Product = productA
                        },
                        new OrderLine4()
                        {
                            Amount  = 20,
                            Product = productB
                        },
                    },
                },
                new Order4()
                {
                    OrderLine = new List <OrderLine4>()
                    {
                        new OrderLine4()
                        {
                            Amount  = 30,
                            Product = productA
                        },
                        new OrderLine4()
                        {
                            Amount  = 40,
                            Product = productB
                        },
                        new OrderLine4()
                        {
                            Amount  = 50,
                            Product = productC
                        },
                    },
                },
            };

            var averageNumberOfOrderLines =
                orders.Average(o => o.OrderLine.Count);

            // 2 + 3 / 2 = 2.5
            Console.WriteLine(averageNumberOfOrderLines);

            // group by
            var result = from o in orders
                         from l in o.OrderLine
                         group l by l.Product // product is key, summary by same instance
                         into p
                         select new
            {
                Product4 = p.Key,
                Amount   = p.Sum(x => x.Amount),
            };

            foreach (var p in result)
            {
                Console.WriteLine($"{p.Product4.Description} summary is {p.Amount}.");
            }
        }