static void Main() { Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture; var mp = new dataMapper(); var _ = mp.getAllCategories(); var __ = mp.getAllProducts(); var ___ = mp.getAllOrders(); // Names of the 5 most expensive products var first = __ .OrderByDescending(p => p.unit_price) .Take(5) .Select(p => p.nome); Console.WriteLine(string.Join(Environment.NewLine, first)); Console.WriteLine(new string('-', 10)); // Number of products in each category var second = __ .GroupBy(p => p.catId) .Select(grp => new { Category = _.First(c => c.Id == grp.Key).NAME, 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 = ___ .GroupBy(o => o.product_id) .Select(grp => new { Product = __.First(p => p.id == grp.Key).nome, Quantities = grp.Sum(grpgrp => grpgrp.quant) }) .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 = ___ .GroupBy(o => o.product_id) .Select(g => new { catId = __.First(p => p.id == g.Key).catId, price = __.First(p => p.id == g.Key).unit_price, quantity = g.Sum(p => p.quant) }) .GroupBy(gg => gg.catId) .Select(grp => new { category_name = _.First(c => c.Id == grp.Key).NAME, 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); }
static void Main() { Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture; var dataMapper = new dataMapper(); var categories = dataMapper.getAllCategories(); var products = dataMapper.getAllProducts(); var orders = dataMapper.getAllOrders(); var fiveMostExpensiveProducts = products .OrderByDescending(p => p.UnitPrice) .Take(5) .Select(p => p.Name); Console.WriteLine(string.Join(Environment.NewLine, fiveMostExpensiveProducts)); Console.WriteLine(new string('-', 10)); var countOfProductsInCategories = products .GroupBy(product => product.CategoryID) .Select(group => new { Category = categories.First(cat => cat.Id == group.Key) .Name, Count = group.Count() }) .ToList(); foreach (var item in countOfProductsInCategories) { Console.WriteLine("{0}: {1}", item.Category, item.Count); } Console.WriteLine(new string('-', 10)); var topProductsByOrderQuantity = orders .GroupBy(order => order.ProductId) .Select(group => new { Product = products.First(product => product.Id == group.Key) .Name, Quantities = group.Sum(count => count.Quantity) }) .OrderByDescending(quantity => quantity.Quantities) .Take(5); foreach (var item in topProductsByOrderQuantity) { Console.WriteLine("{0}: {1}", item.Product, item.Quantities); } Console.WriteLine(new string('-', 10)); var mostProfitableCategory = orders .GroupBy(order => order.ProductId) .Select(group => new { catId = products.First(product => product.Id == group.Key) .CategoryID, price = products.First(product => product.Id == group.Key) .UnitPrice, quantity = group.Sum(product => product.Quantity) }) .GroupBy(category => category.catId) .Select(group => new { category_name = categories.First(category => category.Id == group.Key).Name, total_quantity = group.Sum(grp => grp.quantity * grp.price) }) .OrderByDescending(group => group.total_quantity) .First(); Console.WriteLine("{0}: {1}", mostProfitableCategory.category_name, mostProfitableCategory.total_quantity); }