public static void Main() { using (var context = new FootballEntities()) { var matches = context.InternationalMatches .OrderBy(im => im.MatchDate) .ThenBy(im => im.HostCountry.CountryName) .ThenBy(im => im.GuestCountry.CountryName) .Select(im => new { im.HomeCountryCode, HomeCountry = im.HostCountry.CountryName, GuestCountryCode = im.AwayCountryCode, GuestCountry = im.GuestCountry.CountryName, HostScore = im.HomeGoals, GuestScore = im.AwayGoals, im.League.LeagueName, DateTime = im.MatchDate }); var xmlContent = new XElement("matches"); foreach (var match in matches) { var matchElement = new XElement("match", new XElement("home-country", new XAttribute("code", match.HomeCountryCode), match.HomeCountry), new XElement("away-country", new XAttribute("code", match.GuestCountryCode), match.GuestCountry) ); if (match.GuestScore != null || match.HostScore != null) { matchElement.Add(new XElement("score", match.HostScore, "-", match.GuestScore)); } if (match.LeagueName != null) { matchElement.Add(new XElement("league", match.LeagueName)); } if (match.DateTime != null) { var hasTime = match.DateTime.Value.TimeOfDay.TotalSeconds > 0; Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US"); matchElement.Add(hasTime ? new XAttribute("date-time", match.DateTime.Value.ToString("dd-MMM-yyyy hh:mm")) : new XAttribute("date", match.DateTime.Value.ToString("dd-MMM-yyyy"))); } xmlContent.Add(matchElement); } var xmlDoc = new XDocument(); xmlDoc.Add(xmlContent); xmlDoc.Save("international-matches.xml"); } }
public static void Main() { using (var context = new FootballEntities()) { foreach (var team in context.Teams.ToList()) { Console.WriteLine("Team name: {0}", team.TeamName); } } }
static void Main() { using (var context = new FootballEntities()) { var leagues = context.Leagues .OrderBy(l => l.LeagueName) .Select(l=>new { leagueName = l.LeagueName, teams = l.Teams.OrderBy(t=>t.TeamName).Select(t=>t.TeamName) }); var json = new JavaScriptSerializer().Serialize(leagues); File.WriteAllText("leagues-and-teams.json", json); } }
public static void Main() { var xmlDoc = XElement.Load("../../generate-matches.xml"); using (var context = new FootballEntities()) { var counter = 1; var generateCount = 10; var maxGoals = 5; var startDate = new DateTime(2000, 01, 01); var endDate = new DateTime(2015, 12, 31); string leagueName = null; foreach (var generate in xmlDoc.Elements()) { Console.WriteLine("Processing request #{0} ...", counter++); if (generate.Attribute("generate-count") != null) { generateCount = int.Parse(generate.Attribute("generate-count").Value); } if (generate.Attribute("max-goals") != null) { maxGoals = int.Parse(generate.Attribute("max-goals").Value); } if (generate.Attribute("start-date") != null) { startDate = DateTime.Parse(generate.Element("start-date").Value); } if (generate.Element("end-date") != null) { endDate = DateTime.Parse(generate.Element("end-date").Value); } if (generate.Element("league") != null) { leagueName = generate.Element("league").Value; } //context.TeamMatches.Add(new TeamMatch{ }) } context.SaveChanges(); } }
public static void Main() { var xmlDoc = XElement.Load("../../leagues-and-teams.xml"); using (var context = new FootballEntities()) { var counter = 1; foreach (var league in xmlDoc.Elements()) { Console.WriteLine("Processing league #{0} ...", counter++); var leagueInXml = league.Element("league-name"); bool leagueExistsInQuery = leagueInXml != null; if (leagueInXml != null) { var leagueInDb = context.Leagues.FirstOrDefault(l => l.LeagueName == leagueInXml.Value); if (leagueInDb == null) { var newLeague = new League { LeagueName = league.Element("league-name").Value }; context.Leagues.Add(newLeague); Console.WriteLine("Created league: {0}", newLeague.LeagueName); context.SaveChanges(); } else { Console.WriteLine("Existing league: {0}", leagueInDb.LeagueName); } } var teamsInXml = league.Element("teams"); bool teamsExistsInQuery = teamsInXml != null; if (teamsInXml != null && teamsInXml.Descendants().Any()) { foreach (var team in teamsInXml.Descendants()) { string teamNameInXml = team.Attribute("name").Value; string teamCountryInXml = null; if (team.Attribute("country") != null) { teamCountryInXml = team.Attribute("country").Value; } // check does team exists var teamInDb = context.Teams.FirstOrDefault(t => t.TeamName == teamNameInXml); if (teamInDb != null) { if ((teamInDb.Country != null) && teamInDb.Country.CountryName == (teamCountryInXml ?? "")) { // team exists Console.WriteLine( "Existing team: {0} ({1})", teamNameInXml, teamCountryInXml ?? "no country"); // Check and eventually add team to league if necessary if (leagueExistsInQuery && teamsExistsInQuery) { var teamLeague = teamInDb.Leagues.FirstOrDefault(l => l.LeagueName == leagueInXml.Value); if (teamLeague != null) { Console.WriteLine("Existing team in league: {0} belongs to {1}", teamNameInXml, teamLeague.LeagueName); } else { teamInDb.Leagues.Add(teamLeague); context.SaveChanges(); Console.WriteLine("Added team to league: {0} to league {1}", teamNameInXml, leagueInXml.Value); } } } } else { // not exisiting var newTeam = new Team { TeamName = teamNameInXml }; if (teamCountryInXml != null) { // get country code var teamCountryCode = context.Countries .Where(c => c.CountryName == teamCountryInXml) .Select(c => c.CountryCode).FirstOrDefault(); if (teamCountryCode != null) { newTeam.CountryCode = teamCountryCode; } } context.Teams.Add(newTeam); context.SaveChanges(); Console.WriteLine( "Created team: {0} ({1})", teamNameInXml, teamCountryInXml ?? "no country"); // Just add new team to League if (leagueExistsInQuery && teamsExistsInQuery) { var teamLeague = context.Leagues.FirstOrDefault(l => l.LeagueName == leagueInXml.Value); newTeam.Leagues.Add(teamLeague); context.SaveChanges(); Console.WriteLine("Added team to league: {0} to league {1}", teamNameInXml, leagueInXml.Value); } } } } } context.SaveChanges(); } }