public static void Main(string[] args) { var context = new FootballEntities(); var teamsNames = context.Teams.Select(t => t.TeamName); foreach (var teamName in teamsNames) { Console.WriteLine(teamName); } }
public static void Main(string[] args) { var context = new FootballEntities(); var leaguesWithTeams = context.Leagues .OrderBy(l => l.LeagueName) .Select(l => new { l.LeagueName, Teams = l.Teams.OrderBy(t => t.TeamName).Select(t => t.TeamName) }); var json = JsonConvert.SerializeObject(leaguesWithTeams, Formatting.None, new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }); File.WriteAllText("../../leagues-and-teams.json", json); Console.WriteLine("See the project's folder - leagues-and-teams.json"); }
private static void CreateTeam(FootballEntities context, string teamName, Country country, League league) { Team team; if (country == null) { team = context.Teams.FirstOrDefault(t => (t.Country == null) && t.TeamName == teamName); } else { team = context.Teams.FirstOrDefault( t => t.Country.CountryName == country.CountryName && t.TeamName == teamName); } if (team == null) { team = new Team { TeamName = teamName, Country = country }; context.Teams.Add(team); context.SaveChanges(); Console.WriteLine( "Created team: {0} ({1})", team.TeamName, country != null ? team.Country.CountryName : "no country"); if (league != null) { team.Leagues.Add(league); context.SaveChanges(); Console.WriteLine("Added team to league: {0}", league.LeagueName); } } else { Console.WriteLine( "Existing team: {0} ({1})", team.TeamName, country != null ? team.Country.CountryName : "no country"); if (league != null) { if (!team.Leagues.Any(l => l.LeagueName == league.LeagueName)) { team.Leagues.Add(league); context.SaveChanges(); Console.WriteLine("Added team to league: {0}", league.LeagueName); } else { Console.WriteLine("Existing team in league: {0}", league.LeagueName); } } } }
private static Country GetOrCreateCountry(FootballEntities context, string countryName) { var country = context.Countries.FirstOrDefault(c => c.CountryName == countryName); if (country == null) { country = new Country { CountryName = countryName }; } return country; }
public static void Main(string[] args) { var xmlParrent = new Stack<string>(); using (XmlReader reader = new XmlTextReader("../../leagues-and-teams.xml")) { var context = new FootballEntities(); League league = null; int counter = 0; while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element) { if (reader.Name != "team") { if (reader.Name == "league") { counter++; Console.WriteLine("Processing league #{0}", counter); } xmlParrent.Push(reader.Name); } else { var teamName = reader.GetAttribute("name"); if (teamName == null) { Console.WriteLine("Team name is mandatory"); } else { Country country = null; var countryName = reader.GetAttribute("country"); if (countryName != null) { country = GetOrCreateCountry(context, countryName); } CreateTeam(context, teamName, country, league); } } } if (reader.NodeType == XmlNodeType.Text) { if (xmlParrent.Peek() == "league-name") { var leagueName = reader.Value; league = context.Leagues.FirstOrDefault(l => l.LeagueName == leagueName); if (league != null) { Console.WriteLine("Existing league: {0}", league.LeagueName); } else { league = new League { LeagueName = leagueName }; context.SaveChanges(); Console.WriteLine("Created league: {0}", leagueName); } } } if (reader.NodeType == XmlNodeType.EndElement) { if (reader.Name == "league") { league = null; Console.WriteLine(); } if (reader.Name != "team") { xmlParrent.Pop(); } } } } }
public Generator(FootballEntities context) { this.random = new Random(); this.context = context; }
public static void Main(string[] args) { var xmlParrent = new Stack<string>(); using (XmlReader reader = new XmlTextReader("../../generate-matches.xml")) { var context = new FootballEntities(); var generator = new Generator(context); GenerateConfig generateConfig = null; int count = 0; while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element) { if (reader.Name == "generate") { generateConfig = new GenerateConfig(); var generateCount = reader.GetAttribute("generate-count"); if (generateCount != null) { generateConfig.GenerateCount = int.Parse(generateCount); } var maxGoals = reader.GetAttribute("max-goals"); if (maxGoals != null) { generateConfig.MaxGoals = int.Parse(maxGoals); } } xmlParrent.Push(reader.Name); } if (reader.NodeType == XmlNodeType.Text) { switch (xmlParrent.Peek()) { case "league": generateConfig.League = reader.Value; break; case "start-date": generateConfig.StartDate = DateTime.Parse(reader.Value); break; case "end-date": generateConfig.EndDate = DateTime.Parse(reader.Value); break; } } if (reader.NodeType == XmlNodeType.EndElement) { if (reader.Name == "generate") { count++; Console.WriteLine("Processiong request #" + count); generator.Generate(generateConfig); Console.WriteLine(); } xmlParrent.Pop(); } } } }
public static void Main(string[] args) { var context = new FootballEntities(); var internationalMatches = context.InternationalMatches.OrderBy(im => im.MatchDate) .ThenBy(im => im.HomeCountry) .ThenBy(im => im.AwayCountry) .Select( im => new { im.MatchDate, HomeCountryName = im.HomeCountry.CountryName, HomeCountryCode = im.HomeCountry.CountryCode, AwayCountryName = im.AwayCountry.CountryName, AwayCountryCode = im.AwayCountry.CountryCode, League = im.League.LeagueName, im.HomeGoals, im.AwayGoals }); string file = "../../international-matches.xml"; using (XmlWriter writer = XmlWriter.Create(file)) { writer.WriteStartDocument(); writer.WriteStartElement("matches"); foreach (var match in internationalMatches) { writer.WriteStartElement("match"); if (match.MatchDate.HasValue) { var date = match.MatchDate.Value; if (date.TimeOfDay.TotalSeconds == 0D) { writer.WriteAttributeString("date", date.ToString("dd-MMM-yyy", CultureInfo.InvariantCulture)); } else { writer.WriteAttributeString("date-time", date.ToString("dd-MMM-yyy hh:mm", CultureInfo.InvariantCulture)); } } XmlWriteTeamTag(writer, "home", match.HomeCountryCode, match.HomeCountryName); XmlWriteTeamTag(writer, "away", match.AwayCountryCode, match.AwayCountryName); if (match.League != null) { XmlWriteLeagueTag(writer, match.League); } if (match.HomeGoals.HasValue && match.AwayGoals.HasValue) { XmlWriteScoreTag(writer, match.HomeGoals.Value, match.AwayGoals.Value); } writer.WriteEndElement(); } writer.WriteEndElement(); writer.WriteEndDocument(); } }