static void Main() { var context = new FootballContext(); var leaguesWithTeams = context.Leagues .OrderBy(l => l.LeagueName) .Select(l => new { leagueName = l.LeagueName, teams = l.Teams .OrderBy(t => t.TeamName) .Select(t => t.TeamName) }) .ToList(); var jsonSerializer = new JavaScriptSerializer(); var json = jsonSerializer.Serialize(leaguesWithTeams); File.WriteAllText("leagues-and-teams.json", json); Console.WriteLine("File leagues-and-teams.json exported."); }
private static void AddTeamToLeague(FootballContext context, Team team, League league) { if (league != null) { if (team.Leagues.Contains(league)) { Console.WriteLine("Existing team in league: {0} belongs to {1}", team.TeamName, league.LeagueName); } else { team.Leagues.Add(league); context.SaveChanges(); Console.WriteLine("Added team to league: {0} to league {1}", team.TeamName, league.LeagueName); } } }
private static void CreateTeamsIfNotExists( FootballContext context, IEnumerable<XElement> xTeams, League league) { foreach (var xTeam in xTeams) { // Find the team by team name and country name (if exists) var teamName = xTeam.Attribute("name").Value; var xCountry = xTeam.Attribute("country"); string countryName = null; if (xCountry != null) { countryName = xCountry.Value; } var team = context.Teams .Include(t => t.Leagues) .FirstOrDefault( t => t.TeamName == teamName && t.Country.CountryName == countryName); // Create the team if it does not exists if (team != null) { Console.WriteLine("Existing team: {0} ({1})", team.TeamName, countryName ?? "no country"); } else { // Create a new team in the DB team = new Team() { TeamName = teamName, Country = context.Countries.FirstOrDefault( c => c.CountryName == countryName), }; context.Teams.Add(team); context.SaveChanges(); Console.WriteLine("Created team: {0} ({1})", team.TeamName, countryName ?? "no country"); } AddTeamToLeague(context, team, league); } }
private static League CreateLeagueIfNotExists(FootballContext context, XElement xLeague) { League league = null; var xElementLeagueName = xLeague.Element("league-name"); if (xElementLeagueName != null) { string leagueName = xElementLeagueName.Value; league = context.Leagues.FirstOrDefault(l => l.LeagueName == leagueName); if (league != null) { Console.WriteLine("Existing league: {0}", leagueName); } else { // Create a new league in the DB league = new League() { LeagueName = leagueName }; context.Leagues.Add(league); context.SaveChanges(); Console.WriteLine("Created league: {0}", leagueName); } } return league; }
static void Main() { var context = new FootballContext(); Console.WriteLine(string.Join(", ", context.Teams.Select(t => t.TeamName))); }
private static void ProcessRequest(Request request) { var context = new FootballContext(); var teamsForMatchesQuery = context.Teams.AsQueryable(); var league = context.Leagues.FirstOrDefault(l => l.LeagueName == request.LeagueName); if (request.LeagueName != null) { teamsForMatchesQuery = teamsForMatchesQuery.Where( t => t.Leagues.Select(l => l.Id).Contains(league.Id)); } var teams = teamsForMatchesQuery.ToList(); var rnd = new Random(); var diffDays = (request.EndDate - request.StartDate).Days; for (int i = 0; i < request.GenerateCount; i++) { var match = new TeamMatch(); match.TeamHome = teams[rnd.Next(teams.Count)]; match.TeamAway = teams[rnd.Next(teams.Count)]; match.HomeGoals = rnd.Next(request.MaxGoals + 1); match.AwayGoals = rnd.Next(request.MaxGoals + 1); match.MatchDate = request.StartDate.AddDays(rnd.Next(diffDays + 1)); match.League = league; context.TeamMatches.Add(match); context.SaveChanges(); Console.WriteLine("{0}: {1} - {2}: {3}-{4} ({5})", match.MatchDate.Value.ToString("dd-MMM-yyyy"), match.TeamHome.TeamName, match.TeamAway.TeamName, match.HomeGoals, match.AwayGoals, match.League != null ? match.League.LeagueName : "no league"); } }
static void Main() { var inputXml = XDocument.Load("../../leagues-and-teams.xml"); var xLeagues = inputXml.XPathSelectElements("/leagues-and-teams/league"); var context = new FootballContext(); int leaguesCount = 0; foreach (var xLeague in xLeagues) { Console.WriteLine("Processing league #{0} ...", ++leaguesCount); League league = CreateLeagueIfNotExists(context, xLeague); var xTeams = xLeague.XPathSelectElements("teams/team"); CreateTeamsIfNotExists(context, xTeams, league); Console.WriteLine(); } }
static void Main() { // Ensure date formatting will use the English names Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture; var context = new FootballContext(); var matches = context.InternationalMatches .OrderBy(m => m.MatchDate) .ThenBy(m => m.CountryHome.CountryName) .ThenBy(m => m.CountryAway.CountryName) .Select(m => new { CountryCodeHome = m.CountryHome.CountryCode, CountryNameHome = m.CountryHome.CountryName, CountryCodeAway = m.CountryAway.CountryCode, CountryNameAway = m.CountryAway.CountryName, m.HomeGoals, m.AwayGoals, m.MatchDate, m.League.LeagueName }) .ToList(); var resultXml = new XElement("matches"); foreach (var match in matches) { var matchXml = new XElement("match"); if (match.MatchDate != null) { if (match.MatchDate.Value.TimeOfDay == TimeSpan.Zero) { string date = match.MatchDate.Value.ToString("dd-MMM-yyyy"); matchXml.Add(new XAttribute("date", date)); } else { string dateTime = match.MatchDate.Value.ToString("dd-MMM-yyyy hh:mm"); matchXml.Add(new XAttribute("date-time", dateTime)); } } matchXml.Add(new XElement("home-country", match.CountryNameHome, new XAttribute("code", match.CountryCodeHome))); matchXml.Add(new XElement("away-country", match.CountryNameAway, new XAttribute("code", match.CountryCodeAway))); if (match.HomeGoals != null && match.AwayGoals != null) { string score = match.HomeGoals.Value + "-" + match.AwayGoals.Value; matchXml.Add(new XElement("score", score)); } if (match.LeagueName != null) { matchXml.Add(new XElement("league", match.LeagueName)); } resultXml.Add(matchXml); } var resultXmlDoc = new XDocument(); resultXmlDoc.Add(resultXml); resultXmlDoc.Save("international-matches.xml"); Console.WriteLine("Matches exported to international-matches.xml"); }