コード例 #1
0
        static void Main()
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
            DataMapper             db         = new DataMapper();
            IEnumerable <Category> categories = db.GetCategories();
            IEnumerable <Product>  products   = db.GetProducts();
            IEnumerable <Order>    orders     = db.GetOrders();

            // Names of the 5 most expensive products
            IEnumerable <string> mostExpensive = products
                                                 .OrderByDescending(p => p.UnitPrice)
                                                 .Take(5)
                                                 .Select(p => p.ProductName);

            Console.WriteLine(string.Join(Environment.NewLine, mostExpensive));

            Console.WriteLine(new string('-', 10));

            // Number of products in each category
            var second = products
                         .GroupBy(p => p.ProductCategory)
                         .Select(grp => new { Category = categories.First(c => c.CategoryID == grp.Key).CategoryName, Count = grp.Count() })
                         .ToList();

            foreach (var item in second)
            {
                Console.WriteLine("{0}: {1}", item.Category, item.Count);
            }

            Console.WriteLine(new string('-', 10));

            // The 5 top products (by order quantity)
            var third = orders
                        .GroupBy(o => o.OrderProductID)
                        .Select(grp => new { Product = products.First(p => p.ProductID == grp.Key).ProductName, Quantities = grp.Sum(grpgrp => grpgrp.Quantity) })
                        .OrderByDescending(q => q.Quantities)
                        .Take(5);

            foreach (var item in third)
            {
                Console.WriteLine("{0}: {1}", item.Product, item.Quantities);
            }

            Console.WriteLine(new string('-', 10));

            // The most profitable category
            var category = orders
                           .GroupBy(o => o.OrderProductID)
                           .Select(g => new { catId = products.First(p => p.ProductID == g.Key).ProductCategory, price = products.First(p => p.ProductID == g.Key).UnitPrice, quantity = g.Sum(p => p.Quantity) })
                           .GroupBy(gg => gg.catId)
                           .Select(grp => new { category_name = categories.First(c => c.CategoryID == grp.Key).CategoryName, total_quantity = grp.Sum(g => g.quantity * g.price) })
                           .OrderByDescending(g => g.total_quantity)
                           .First();

            Console.WriteLine("{0}: {1}", category.category_name, category.total_quantity);
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: AlexanderKrustev/SoftUni
        static void Main()
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
            DataMapper db = new DataMapper();
            IEnumerable<Category> categories = db.GetCategories();
            IEnumerable<Product> products = db.GetProducts();
            IEnumerable<Order> orders = db.GetOrders();

            // Names of the 5 most expensive products
               IEnumerable<string> mostExpensive = products
                .OrderByDescending(p => p.UnitPrice)
                .Take(5)
                .Select(p => p.ProductName);
            Console.WriteLine(string.Join(Environment.NewLine, mostExpensive));

            Console.WriteLine(new string('-', 10));

            // Number of products in each category
            var second = products
                .GroupBy(p => p.ProductCategory)
                .Select(grp => new { Category = categories.First(c => c.CategoryID == grp.Key).CategoryName, Count = grp.Count() })
                .ToList();
            foreach (var item in second)
            {
                Console.WriteLine("{0}: {1}", item.Category, item.Count);
            }

            Console.WriteLine(new string('-', 10));

            // The 5 top products (by order quantity)
            var third = orders
                .GroupBy(o => o.OrderProductID)
                .Select(grp => new { Product = products.First(p => p.ProductID == grp.Key).ProductName, Quantities = grp.Sum(grpgrp => grpgrp.Quantity) })
                .OrderByDescending(q => q.Quantities)
                .Take(5);
            foreach (var item in third)
            {
                Console.WriteLine("{0}: {1}", item.Product, item.Quantities);
            }

            Console.WriteLine(new string('-', 10));

            // The most profitable category
            var category = orders
                .GroupBy(o => o.OrderProductID)
                .Select(g => new { catId = products.First(p => p.ProductID== g.Key).ProductCategory, price = products.First(p => p.ProductID == g.Key).UnitPrice, quantity = g.Sum(p => p.Quantity) })
                .GroupBy(gg => gg.catId)
                .Select(grp => new { category_name = categories.First(c => c.CategoryID == grp.Key).CategoryName, total_quantity = grp.Sum(g => g.quantity * g.price) })
                .OrderByDescending(g => g.total_quantity)
                .First();
            Console.WriteLine("{0}: {1}", category.category_name, category.total_quantity);
        }
コード例 #3
0
        public static void Main()
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
            var dataMapper = new DataMapper();
            var categories = dataMapper.GetCategories();
            var products = dataMapper.GetProducts();
            var orders = dataMapper.GetOrders();

            // Print names of the 5 most expensive products
            var firstFiveMostExpensiveProducts = products
                .OrderByDescending(p => p.UnitPrice)
                .Take(5)
                .Select(p => p.Nome);
            Console.WriteLine(string.Join(Environment.NewLine, firstFiveMostExpensiveProducts));

            Console.WriteLine(new string('-', 10));

            // Print number of products in each category
            var productsInEachCategory = products
                .GroupBy(p => p.CategoryId)
                .Select(group =>
                new
                {
                    Category = categories.First(cat => cat.Id == group.Key).Name, Count = group.Count()
                })
                .ToList();

            foreach (var product in productsInEachCategory)
            {
                Console.WriteLine("{0}: {1}", product.Category, product.Count);
            }

            Console.WriteLine(new string('-', 10));

            // Print the 5 top products (by Order quantity)
            var topFiveProductsByOrderQuantity = orders
                .GroupBy(
                    order => order.ProductId)
                    .Select(group =>
                    new
                    {
                        Product = products.First(p => p.Id == group.Key).Nome,
                        Quantities = group.Sum(order => order.Quantity)
                    })
                .OrderByDescending(q => q.Quantities)
                .Take(5);

            foreach (var item in topFiveProductsByOrderQuantity)
            {
                Console.WriteLine("{0}: {1}", item.Product, item.Quantities);
            }

            Console.WriteLine(new string('-', 10));

            // Print the most profitable category
            var category = orders
                .GroupBy(id => id.ProductId)
                .Select(group => new
                {
                    catId = products.First(p => p.Id == group.Key).CategoryId,
                    price = products.First(p => p.Id == group.Key).UnitPrice,
                    quantity = group.Sum(p => p.Quantity)
                })
                .GroupBy(group => group.catId)
                .Select(group => new
                {
                    CategoryName = categories.First(c => c.Id == group.Key).Name,
                    TotalQuantity = group.Sum(g => g.quantity * g.price)
                })
                .OrderByDescending(g => g.TotalQuantity)
                .First();
            Console.WriteLine("{0}: {1}", category.CategoryName, category.TotalQuantity);
        }