コード例 #1
0
        public static void GetCustomersInCategory(string name)
        {
            IEnumerable <Customer> customers  = CustomerDb.GetAllCustomers();
            IEnumerable <Category> categories = CategoryDb.GetAllCategories();


            IEnumerable <CustomersGoods> customerGoodsDbs = CustomerGoodsDb.GetCustomerGoods();
            var listOfCategories = categories.Where(p => p.Name == name);

            var needProducts = listOfCategories.Join(customerGoodsDbs,
                                                     c => c.Product.IdGoods, cg => cg.Product.IdGoods, (c, cg) => new { Id = cg.Customer.IdCustomer });

            var result = needProducts.Join(customers, arg => arg.Id, c => c.IdCustomer,
                                           (arg, c) => new { Name = c.Name });
        }
コード例 #2
0
        public static void GetCustomerWithMostCategories()
        {
            var customersHasGoods = CustomerGoodsDb.GetCustomerGoods();
            var categories        = CategoryDb.GetAllCategories();

            var subresult = customersHasGoods.Join(
                categories,
                cg => cg.Product.IdGoods,
                category => category.Product.IdGoods,
                (cg, category) => new { Category = category, Customer = cg.Customer, Goods = category.Product });

            var preres = subresult.GroupBy(r => r.Customer.Surname).Select(g => new { Name = g.Key, Count = g.Count() });
            var res    = preres.OrderByDescending(n => n.Count).First();

            Console.WriteLine("Максимально разнообразный {0} - у него товары в {1} категориях", res.Name, res.Count);
        }