Esempio n. 1
0
        public void Linq001()
        {
            int[] arr = { 100, 1000, 10000, 100000 };

            foreach (var x in arr)
            {
                LinqSamples.WriteHeader($"x is {x}");

                var products = this.dataSource.Customers
                               .Where(cust => cust.Orders.Sum(order => order.Total) > x)
                               .Select(cust => $"Name = {cust.CompanyName}, Order's total price = {cust.Orders.Sum(order => order.Total)}");

                LinqSamples.Show(products);
            }
        }
Esempio n. 2
0
        public void Linq002()
        {
            var сustomers = this.dataSource.Customers;
            var suppliers = this.dataSource.Suppliers;

            var result =
                from supplier in suppliers
                from customer in сustomers
                where supplier.Country == customer.Country &&
                supplier.City == customer.City
                select $"Company {customer.CompanyName} and supplier {supplier.SupplierName} are in the same city {supplier.City} and country {supplier.Country}";

            LinqSamples.WriteHeader("Ingroupped");

            LinqSamples.Show(result);

            var grouppedResult =
                from grouppedCollection in (
                    from supplier in suppliers
                    from customer in сustomers
                    where supplier.Country == customer.Country &&
                    supplier.City == customer.City
                    select new
            {
                Customer = customer,
                Supplier = supplier,
            })
                group grouppedCollection by grouppedCollection.Customer.Country;

            LinqSamples.WriteHeader("Groupped");

            if (grouppedResult.Any())
            {
                foreach (var p in grouppedResult)
                {
                    ObjectDumper.Write($"In {p.Key} there's");

                    foreach (var item in p)
                    {
                        ObjectDumper.Write($"    company {item.Customer.CompanyName} and supplier {item.Supplier.SupplierName}");
                    }
                }
            }
            else
            {
                Console.WriteLine("No such objects");
            }
        }
Esempio n. 3
0
        public void Linq010()
        {
            var clients = this.dataSource.Customers;

            LinqSamples.WriteHeader("Mounthly");

            var mounthly =
                from order in clients.SelectMany(client => client.Orders).Select(o => o.OrderDate)
                group order by order.Month
                into groupped
                select
                new KeyValuePair <string, int>(
                    CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(groupped.Key),
                    groupped.Count());

            this.Show(mounthly);

            LinqSamples.WriteHeader("Yearly");

            var yearly = from order in clients.SelectMany(client => client.Orders).Select(o => o.OrderDate)
                         group order by order.Year
                         into groupped
                         select
                         new KeyValuePair <int, int>(groupped.Key,
                                                     groupped.Count());

            LinqSamples.Show(yearly.OrderBy(g => g.Key));

            LinqSamples.WriteHeader("Bothly");

            var bothly = from order in clients.SelectMany(client => client.Orders).Select(o => o.OrderDate)
                         group order by new { year = order.Year, mounth = order.Month }
            into groupped
                select
            new
            {
                Key   = new KeyValuePair <int, int>(groupped.Key.year, groupped.Key.mounth),
                Value = groupped.Count(),
            };

            foreach (var item in bothly.OrderBy(g => g.Key.Key).ThenBy(g => g.Key.Value))
            {
                ObjectDumper.Write($"{item.Key.Key}, {CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(item.Key.Value)} - {item.Value}");
            }
        }
Esempio n. 4
0
        public void Linq009()
        {
            var profitability = from customer in this.dataSource.Customers
                                group customer by customer.City
                                into groupped
                                select
                                new KeyValuePair <string, decimal>(groupped.Key,
                                                                   groupped.SelectMany(c => c.Orders).Average(o => o.Total));

            LinqSamples.WriteHeader("Profitability");
            LinqSamples.Show(profitability);

            var intensity = from customer in this.dataSource.Customers
                            group customer by customer.City
                            into groupped
                            select
                            new KeyValuePair <string, double>(groupped.Key, groupped.Average(customer => customer.Orders.Length));

            LinqSamples.WriteHeader("Intensity");
            LinqSamples.Show(intensity);
        }