Created
March 27, 2019 13:04
-
-
Save SonnyRR/ac8dda5a1c4d441e45aa9d5d6a697cfa to your computer and use it in GitHub Desktop.
Exercise from DB-Advanced @ SoftUni March 2019
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static string GetCategoriesByProductsCount(ProductShopContext context) | |
{ | |
var categories = context.Categories | |
.OrderByDescending(c => c.CategoryProducts.Count) | |
.Select(x => new | |
{ | |
Category = x.Name, | |
ProductsCount = x.CategoryProducts.Count, | |
AveragePrice = $"{x.CategoryProducts.Average(c => c.Product.Price):F2}", | |
TotalRevenue = $"{x.CategoryProducts.Sum(c => c.Product.Price)}" | |
}) | |
.ToList(); | |
string json = JsonConvert.SerializeObject(categories, | |
new JsonSerializerSettings() | |
{ | |
ContractResolver = new DefaultContractResolver() | |
{ | |
NamingStrategy = new CamelCaseNamingStrategy(), | |
}, | |
Formatting = Formatting.Indented | |
} | |
); | |
return json; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment