private List<DataChampion> MostUsedChampions(Record data, bool PreChange, int iditem, List<int> champions) { List<Tuple<ChampionRecord, float>> dataPerChampion = new List<Tuple<ChampionRecord, float>>(); foreach (int champId in champions) { var champRecord = data.RecordsByChampions.ChampionsRecords.Where(r => r.ChampionId == champId).Single(); var recordItem = champRecord.ItemsRecord.Items.Where(i => i.ItemID == iditem).Single(); var totalPlayersChamp = _matches.Where(m => m.Pre_Change == PreChange).SelectMany(ma => ma.Players).Where(p => p.ChampionUsed.ChampionId == champId).ToList().Count; float percentageUsed = 0; if (totalPlayersChamp > 0) { if (PreChange) { percentageUsed = (recordItem.PreChangeRecord / (float)totalPlayersChamp) * 100f; } else { percentageUsed = (recordItem.PostChangeRecord / (float)totalPlayersChamp) * 100f; } } dataPerChampion.Add(new Tuple<ChampionRecord, float>(champRecord, percentageUsed)); } dataPerChampion.Sort((x, y) => y.Item2.CompareTo(x.Item2)); var mostUsed = dataPerChampion.Take(5).ToList(); var listMostUsed = mostUsed.Select(m => new DataChampion() { Name = _context.Champions.Where(c => c.ChampionId == m.Item1.ChampionId).Single().Name, Value = m.Item2 }).ToList(); return listMostUsed; }
public void CreateRecord() { try { Record record = new Record(); record.Matches = _matches.Count; record.Players = _matches.Select(m => m.Players).ToList().Count; var matchesData = _matches.ToList(); var matchesPreChange = matchesData.Where(m => m.Pre_Change == true).ToList(); var matchesPostChange = matchesData.Where(m => m.Pre_Change == false).ToList(); Record_Buy_Percentage recordBuy = new Record_Buy_Percentage(); AllItemsRecord itemsRecord = new AllItemsRecord(); var listOfItemRecords = new List<ItemRecord>(); foreach (var key in APItems.Items.Keys) { var item = new ItemRecord() { ItemID = APItems.Items[key], PreChangeRecord = matchesPreChange.SelectMany(m => m.Players) .Where(p => p.ItemsBought .Contains(APItems.Items[key].ToString())) .ToList() .Count, PostChangeRecord = matchesPostChange.SelectMany(m => m.Players) .Where(p => p.ItemsBought .Contains(APItems.Items[key].ToString())) .ToList() .Count }; listOfItemRecords.Add(item); } itemsRecord.Items = listOfItemRecords; recordBuy.ItemsRecord = itemsRecord; record.RecordsByBuyPercentage = recordBuy; Record_Champions recordChampions = new Record_Champions(); var listOfChampionRecords = new List<ChampionRecord>(); var championIds = _context.Champions.Select(c => c.ChampionId).ToList(); foreach (int championId in championIds) { var championRecord = new ChampionRecord(); championRecord.ChampionId = championId; var allitemsChampionRecord = new AllItemsRecord(); var listRecords = new List<ItemRecord>(); foreach (var key in APItems.Items.Keys) { var item = new ItemRecord() { ItemID = APItems.Items[key], PreChangeRecord = matchesPreChange.SelectMany(m => m.Players) .Where(p => p.ChampionUsed.ChampionId == championId && p.ItemsBought.Contains(APItems.Items[key].ToString())) .ToList() .Count, PostChangeRecord = matchesPostChange.SelectMany(m => m.Players) .Where(p => p.ChampionUsed.ChampionId == championId && p.ItemsBought.Contains(APItems.Items[key].ToString())) .ToList() .Count }; listRecords.Add(item); } allitemsChampionRecord.Items = listRecords; championRecord.ItemsRecord = allitemsChampionRecord; listOfChampionRecords.Add(championRecord); } recordChampions.ChampionsRecords = listOfChampionRecords; record.RecordsByChampions = recordChampions; _context.Records.Add(record); _context.SaveChanges(); } catch (DbEntityValidationException e) { foreach (var eve in e.EntityValidationErrors) { Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } } throw; } }