Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 06. Export Sold Products
- public static string GetSoldProducts(ProductShopContext context)
- {
- var soldProducts = context.Users
- .Where(u => u.ProductsSold.Any(ps => ps.Buyer != null))
- .Select(u => new
- {
- firstName = u.FirstName,
- lastName = u.LastName,
- soldProducts = u.ProductsSold
- .Where(ps => ps.Buyer != null)
- .Select(ps => new
- {
- name = ps.Name,
- price = ps.Price,
- buyerFirstName = ps.Buyer.FirstName,
- buyerLastName = ps.Buyer.LastName
- })
- })
- .OrderBy(u => u.lastName)
- .ThenBy(u => u.firstName)
- .ToList();
- var jsonSoldProducts = JsonConvert.SerializeObject(soldProducts, Formatting.Indented);
- return jsonSoldProducts;
- }
- // 07. Export Categories By Products Count
- public static string GetCategoriesByProductsCount(ProductShopContext context)
- {
- var categoriesByProductsCount = context.Categories
- .Select(x => new
- {
- category = x.Name,
- productsCount = x.CategoryProducts.Count(),
- averagePrice = $@"{x.CategoryProducts
- .Sum(p => p.Product.Price) / x.CategoryProducts.Count():F2}",
- totalRevenue = $"{x.CategoryProducts.Sum(p => p.Product.Price):F2}"
- })
- .OrderByDescending(x => x.productsCount)
- .ToList();
- var jsonCategoriesByProductsCount = JsonConvert.SerializeObject(categoriesByProductsCount, Formatting.Indented);
- return jsonCategoriesByProductsCount;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement