private void SavePositions(EstimatedRankings points, Activity isActive) { if (!_positionsRepository.AsQueryable().Where(x => x.ID == _currentSeason.ToString()).Any()) { KeyValuePair <Teams, int?>[] teamPoints = new KeyValuePair <Teams, int?> [44]; for (int i = 0; i < 44; i++) { PropertyInfo propInfo = points.GetType().GetProperty(allTeams[i].Name.Replace(' ', '_').Replace('.', '_')); teamPoints[i] = new KeyValuePair <Teams, int?>(allTeams[i], Convert.ToInt32(propInfo.GetValue(points))); } teamPoints = teamPoints.OrderByDescending(m => m.Value).ToArray(); for (int i = 0; i < teamPoints.Length; i++) { if (teamPoints[i].Value == 0) { teamPoints[i] = new KeyValuePair <Teams, int?>(teamPoints[i].Key, null); } else { teamPoints[i] = new KeyValuePair <Teams, int?>(teamPoints[i].Key, i + 1); } } Positions positions = new Positions(); positions.ID = _currentSeason.ToString(); for (int i = 0; i < allTeams.Length; i++) { int? pkt = teamPoints.Where(m => m.Key == allTeams[i]).First().Value; PropertyInfo propInfo = new Positions().GetType().GetProperty(allTeams[i].Name.Replace(' ', '_').Replace('.', '_')); if (pkt != null && pkt != 0) { propInfo.SetValue(positions, Convert.ToInt32(teamPoints.Where(m => m.Key == allTeams[i]).First().Value)); } else { propInfo.SetValue(positions, null); } } _positionsRepository.Add(positions); _positionsRepository.SaveChanges(); } }