static void Main() { Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture; DataMapper dataMapper = new DataMapper(); IEnumerable <Category> takeCategories = dataMapper.getAllCategories(); IEnumerable <Product> takeProducts = dataMapper.getAllProducts(); IEnumerable <Order> takeOrders = dataMapper.getAllOrders(); // Names of the 5 most expensive products var first = takeProducts .OrderByDescending(p => p.UnitPrice) .Take(5) .Select(p => p.Name); Console.WriteLine(string.Join(Environment.NewLine, first)); Console.WriteLine(new string('-', 10)); // Number of products in each category var second = takeProducts .GroupBy(p => p.CatId) .Select(grp => new { Category = takeCategories.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 = takeOrders .GroupBy(o => o.ProductId) .Select(grp => new { Product = takeProducts.First(p => p.Id == grp.Key).Name, 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 = takeOrders .GroupBy(o => o.ProductId) .Select(g => new { CatId = takeProducts.First(p => p.Id == g.Key).CatId, price = takeProducts.First(p => p.Id == g.Key).UnitPrice, quantity = g.Sum(p => p.Quantity) }) .GroupBy(gg => gg.CatId) .Select(grp => new { categoryName = takeCategories.First(c => c.Id == grp.Key).Name, totalQuantity = grp.Sum(g => g.quantity * g.price) }) .OrderByDescending(g => g.totalQuantity) .First(); Console.WriteLine("{0}: {1}", category.categoryName, category.totalQuantity); }
static void Main() { Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture; var dataMapper = new DataMapper(); var allCategories = dataMapper.getAllCategories(); var allProducts = dataMapper.getAllProducts(); var allOrders = dataMapper.getAllOrders(); // Names of the 5 most expensive products var first = allProducts .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 = allProducts .GroupBy(p => p.catId) .Select(grp => new { Category = allCategories.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 = allOrders .GroupBy(o => o.product_id) .Select(grp => new { Product = allProducts.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 = allOrders .GroupBy(o => o.product_id) .Select(g => new { catId = allProducts.First(p => p.id == g.Key).catId, price = allProducts.First(p => p.id == g.Key).unit_price, quantity = g.Sum(p => p.quant) }) .GroupBy(gg => gg.catId) .Select(grp => new { category_name = allCategories.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 allCategories = dataMapper.getAllCategories(); var allProducts = dataMapper.getAllProducts(); var allOrders = dataMapper.getAllOrders(); // Names of the 5 most expensive products var FiveMostExpensiveProducts = allProducts .OrderByDescending(p => p.UnitPrice) .Take(5) .Select(p => p.Name); Console.WriteLine(string.Join(Environment.NewLine, FiveMostExpensiveProducts)); Console.WriteLine(new string('-', 10)); // Number of products in each category var NumsOfProductsForEachCategory = allProducts .GroupBy(p => p.CategoryId) .Select(grp => new { Category = allCategories.First(c => c.Id == grp.Key).Name, Count = grp.Count() }).ToList(); foreach (var item in NumsOfProductsForEachCategory) { Console.WriteLine("{0}: {1}", item.Category, item.Count); } Console.WriteLine(new string('-', 10)); // The 5 top products (by order quantity) var FiveTopProductsOrderedByQuantity = allOrders .GroupBy(o => o.ProductId) .Select(grp => new { Product = allProducts.First(p => p.Id == grp.Key).Name, Quantities = grp.Sum(grpgrp => grpgrp.Quantity) }) .OrderByDescending(q => q.Quantities) .Take(5); foreach (var item in FiveTopProductsOrderedByQuantity) { Console.WriteLine("{0}: {1}", item.Product, item.Quantities); } Console.WriteLine(new string('-', 10)); // The most profitable category var category = allOrders .GroupBy(o => o.ProductId) .Select(g => new { CategoryId = allProducts.First(p => p.Id == g.Key).CategoryId, Price = allProducts.First(p => p.Id == g.Key).UnitPrice, Quantity = g.Sum(q => q.Quantity) }) .GroupBy(g => g.CategoryId) .Select(grp => new { CategoryName = allCategories.First(c => c.Id == grp.Key).Name, TotalQuantity = grp.Sum(g => g.Quantity * g.Price) }) .OrderByDescending(g => g.TotalQuantity) .First(); Console.WriteLine("{0}: {1}", category.CategoryName, category.TotalQuantity); }
static void Main() { Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture; var dataMapper = new DataMapper(); var categories = dataMapper.getAllCategories(); var products = dataMapper.getAllProducts(); var orders = dataMapper.getAllOrders(); // Names of the 5 most expensive products var fiveMostExpensiveProducts = products .OrderByDescending(product => product.UnitPrice) .Take(5) .Select(product => product.Name); Console.WriteLine(string.Join(Environment.NewLine, fiveMostExpensiveProducts)); Console.WriteLine(new string('-', 10)); // Number of products in each category var numberOfProductsInCategory = products .GroupBy(product => product.CategoryId) .Select(group => new { Category = categories.First(category => category.Id == group.Key).Name, Count = group.Count() }) .ToList(); foreach (var product in numberOfProductsInCategory) { Console.WriteLine("{0}: {1}", product.Category, product.Count); } Console.WriteLine(new string('-', 10)); // The 5 top products (by order quantity) var fiveTopProductsByOrderQuantity = orders .GroupBy(order => order.ProductId) .Select(group => new { Product = products.First(product => product.Id == group.Key).Name, Quantity = group.Sum(item => item.Quantity) }) .OrderByDescending(group => group.Quantity) .Take(5); foreach (var product in fiveTopProductsByOrderQuantity) { Console.WriteLine("{0}: {1}", product.Product, product.Quantity); } Console.WriteLine(new string('-', 10)); // The most profitable category var mostProfitableCategory = orders .GroupBy(order => order.ProductId) .Select(group => new { categatyId = products.First(product => product.Id == group.Key).CategoryId, Price = products.First(product => product.Id == group.Key).UnitPrice, Quantity = group.Sum(product => product.Quantity) }) .GroupBy(order => order.categatyId) .Select(group => new { CategoryName = categories.First(category => category.Id == group.Key).Name, TotalQuantity = group.Sum(item => item.Quantity * item.Price) }) .OrderByDescending(group => group.TotalQuantity) .First(); Console.WriteLine("{0}: {1}", mostProfitableCategory.CategoryName, mostProfitableCategory.TotalQuantity); }