Advertisement
simonradev

Untitled

Dec 10th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.41 KB | None | 0 0
  1. public static string ExportCategoryStatistics(FastFoodDbContext context, string categoriesString)
  2.         {
  3.             string[] categories = categoriesString.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
  4.  
  5.             var categoriesWithItem = context.Categories
  6.                                     .Where(c => categories.Contains(c.Name))
  7.                                     .Select(c => new
  8.                                     {
  9.                                         Name = c.Name,
  10.                                         MostPopularItem = c.Items
  11.                                                     .OrderByDescending(i => i.Price * i.OrderItems.Sum(oi => oi.Quantity))
  12.                                                     .Select(i => new
  13.                                                     {
  14.                                                         Name = i.Name,
  15.                                                         TotalMade = i.Price * i.OrderItems.Sum(oi => oi.Quantity),
  16.                                                         TimesSold = i.OrderItems.Sum(oi => oi.Quantity)
  17.                                                     }).FirstOrDefault()
  18.                                     })
  19.                                     .OrderByDescending(c => c.MostPopularItem.TotalMade)
  20.                                     .ThenByDescending(c => c.MostPopularItem.TimesSold)
  21.                                     .ToArray();
  22.  
  23.             XElement[] elements = categoriesWithItem
  24.                                   .Select(c => new XElement("Category",
  25.                                                             new XElement("Name", c.Name),
  26.                                                             new XElement("MostPopularItem",
  27.                                                                          new XElement("Name", c.MostPopularItem.Name),
  28.                                                                          new XElement("TotalMade", c.MostPopularItem.TotalMade.ToString("f2")),
  29.                                                                          new XElement("TimesSold", c.MostPopularItem.TimesSold)
  30.                                                                          ))).ToArray();
  31.             XElement root = new XElement("Categories", elements);
  32.             XDocument xDocument = new XDocument(root);
  33.             string result = xDocument.ToString();
  34.             return result;
  35.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement