Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static string ExportCategoryStatistics(FastFoodDbContext context, string categoriesString)
- {
- string[] categories = categoriesString.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
- var categoriesWithItem = context.Categories
- .Where(c => categories.Contains(c.Name))
- .Select(c => new
- {
- Name = c.Name,
- MostPopularItem = c.Items
- .OrderByDescending(i => i.Price * i.OrderItems.Sum(oi => oi.Quantity))
- .Select(i => new
- {
- Name = i.Name,
- TotalMade = i.Price * i.OrderItems.Sum(oi => oi.Quantity),
- TimesSold = i.OrderItems.Sum(oi => oi.Quantity)
- }).FirstOrDefault()
- })
- .OrderByDescending(c => c.MostPopularItem.TotalMade)
- .ThenByDescending(c => c.MostPopularItem.TimesSold)
- .ToArray();
- XElement[] elements = categoriesWithItem
- .Select(c => new XElement("Category",
- new XElement("Name", c.Name),
- new XElement("MostPopularItem",
- new XElement("Name", c.MostPopularItem.Name),
- new XElement("TotalMade", c.MostPopularItem.TotalMade.ToString("f2")),
- new XElement("TimesSold", c.MostPopularItem.TimesSold)
- ))).ToArray();
- XElement root = new XElement("Categories", elements);
- XDocument xDocument = new XDocument(root);
- string result = xDocument.ToString();
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement