Skip to content

Instantly share code, notes, and snippets.

@SonnyRR
Created March 27, 2019 13:04
Show Gist options
  • Save SonnyRR/ac8dda5a1c4d441e45aa9d5d6a697cfa to your computer and use it in GitHub Desktop.
Save SonnyRR/ac8dda5a1c4d441e45aa9d5d6a697cfa to your computer and use it in GitHub Desktop.
Exercise from DB-Advanced @ SoftUni March 2019
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