public override RevenueListViewModel Initialize() { var retVal = base.Initialize(); var list = mOrderLinesService.GetAll(); var dict = new Dictionary <int, AreaBestseller>(); foreach (var orderLine in list) { foreach (var area in orderLine.Order.Driver.Areas) { if (!dict.ContainsKey(area.Id)) { dict.Add(area.Id, new AreaBestseller() { Area = area, Revenue = orderLine.Amount * orderLine.Article.Price }); continue; } dict[area.Id].Revenue += orderLine.Amount * orderLine.Article.Price; } } var bestsellers = from areaBestseller in dict orderby areaBestseller.Value.Revenue descending select areaBestseller.Value; var topTen = bestsellers.Take(10); int i = 1; foreach (var areaBestseller in topTen) { areaBestseller.Rank = i; i++; } retVal.AreaBestsellers = new ObservableCollection <AreaBestseller>(topTen); return(retVal); }
public override BestsellerViewModel Initialize() { var retVal = base.Initialize(); var orderLines = mOrderLinesService.GetAll(); var dict = new Dictionary <int, Bestseller>(); foreach (var orderLine in orderLines) { if (dict.ContainsKey(orderLine.Article.Id)) { var article = dict[orderLine.Article.Id]; article.Amount += orderLine.Amount; continue; } dict.Add(orderLine.Article.Id, new Bestseller() { Amount = orderLine.Amount, Article = orderLine.Article }); } var bestsellers = from seller in dict orderby seller.Value.Revenue descending select seller.Value; var bestsellerList = bestsellers.Take(10).ToList(); int i = 1; foreach (var bestseller in bestsellerList) { bestseller.Rank = i; i++; } retVal.Bestsellers = new ObservableCollection <Bestseller>(bestsellerList); return(retVal); }