示例#1
0
        public void Linq004()
        {
            var result = this.dataSource.Customers
                         .Select(cust => $"{cust.CompanyName} is client since {(cust.Orders.Any() ? cust.Orders.Min(order => order.OrderDate).ToString("MMMM yyyy") : "never")}");

            LinqSamples.Show(result);
        }
示例#2
0
        public void Linq006()
        {
            var result = this.dataSource.Customers
                         .Where(cust => string.IsNullOrWhiteSpace(cust.PostalCode) ||
                                cust.PostalCode.Any(c => !char.IsDigit(c)) ||
                                string.IsNullOrWhiteSpace(cust.Region) ||
                                !cust.Phone.StartsWith("(", StringComparison.InvariantCultureIgnoreCase))
                         .Select(cust => $"Customer {cust.CompanyName} have postal code {cust.PostalCode}, region {cust.Region}, phone {cust.Phone}");

            LinqSamples.Show(result);
        }
示例#3
0
        public void Linq005()
        {
            var result = this.dataSource.Customers
                         .Where(cust => cust.Orders.Any())
                         .ToDictionary(cust => cust,
                                       cust => cust.Orders.OrderBy(order => order.OrderDate).FirstOrDefault())
                         .OrderBy(kvp => kvp.Value.OrderDate.Year)
                         .ThenBy(kvp => kvp.Value.OrderDate.Month)
                         .ThenByDescending(kvp => kvp.Key.Orders.Sum(order => order.Total))
                         .ThenBy(kvp => kvp.Key.CompanyName)
                         .Select(dict => $"{dict.Key.CompanyName} is client since {dict.Value.OrderDate.ToString("yyyy MMMM")}, total is {dict.Key.Orders.Sum(order => order.Total)}");

            LinqSamples.Show(result);
        }
示例#4
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);
            }
        }
示例#5
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");
            }
        }
示例#6
0
        public void Linq007()
        {
            var result =
                from product in this.dataSource.Products
                group product by product.Category
                into groupedByCategoryProducts
                select
                new KeyValuePair <string, IEnumerable <KeyValuePair <decimal, IEnumerable <Product> > > >(
                    groupedByCategoryProducts.Key,
                    from p in groupedByCategoryProducts
                    group p by p.UnitsInStock
                    into asd
                    select new KeyValuePair <decimal, IEnumerable <Product> >(asd.Key, asd.OrderBy(a => a.UnitPrice)));

            LinqSamples.Show(result);
        }
示例#7
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}");
            }
        }
示例#8
0
        public void Linq003()
        {
            int[] arr = { 100, 1000, 10000, 100000 };

            foreach (var x in arr)
            {
                ObjectDumper.Write("");
                ObjectDumper.Write("");
                ObjectDumper.Write($"x is {x}");
                ObjectDumper.Write("");
                ObjectDumper.Write("");

                var result = this.dataSource.Customers
                             .Where(cust => cust.Orders.Any(order => order.Total > x))
                             .Select(cust => $"Customer {cust.CompanyName} have order with {cust.Orders.First(order => order.Total > x).Total} total");

                LinqSamples.Show(result);
            }
        }
示例#9
0
        public void Linq008()
        {
            const int min = 10;
            const int max = 100;

            var dict = new Dictionary <string, uint>
            {
                { "Low", 0 },
                { "Middle", 1 },
                { "High", 2 },
            };

            var result = this.dataSource.Products
                         .GroupBy(product => LinqSamples.GetRange(product, min, max),
                                  product => $"Name = {product.ProductName}, price is {product.UnitPrice.ToString(".##")}")
                         .OrderBy(kvp => dict[kvp.Key]);

            LinqSamples.Show(result);
        }
示例#10
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);
        }