private static void BuildLeagueDictionary() { ObjectResult <CEventsGetLeagues_Result> leagues = context.CEventsGetLeagues(); foreach (CEventsGetLeagues_Result element in leagues.ToList()) { if (!Dicts.RegionDict.ContainsKey(element.Region)) { EnsureRegionExists(element.Region); } Sport sport = context.Sports.Where((s) => s.ID == element.SportID).FirstOrDefault(); if (sport == null || Dicts.LeagueDict.ContainsKey(new Tuple <string, Region, Sport>(element.League, Dicts.RegionDict[element.Region], sport))) { continue; } bool found = false; foreach (League item in context.Leagues.ToList()) { //item.Region = context.Regions.Where(r => r.ID == item.RegionID).FirstOrDefault(); if (item.RegionID == Dicts.RegionDict[element.Region].ID && item.SportID == sport.ID) { LeagueComparisonQueue.Enqueue(new CompareLeague() { RegionID = Dicts.RegionDict[element.Region].ID, First = element.League, Second = item.Name, SportID = sport.ID }); found = true; break; } } if (!found) { League newLeague = new League(); newLeague.Name = element.League; newLeague.SportID = sport.ID; newLeague.RegionID = Dicts.RegionDict[element.Region].ID; context.Leagues.Add(newLeague); context.SaveChanges(); Dicts.LeagueDict.Add(new Tuple <string, Region, Sport>(element.League, Dicts.RegionDict[element.Region], sport), newLeague); } } }