static void Main() { var footballEntities = new FootballEntities(); var footballTeams = footballEntities.Teams .Select(t => t.TeamName); foreach (var footballTeam in footballTeams) { Console.WriteLine(footballTeam); } }
static void Main() { var footballEntities = new FootballEntities(); var leaguesWithTeams = footballEntities.Leagues .OrderBy(l => l.LeagueName) .Select(l => new { leagueName = l.LeagueName, teams = l.Teams.OrderBy(t => t.TeamName).Select(t => t.TeamName) }); var leaguesWithTeamsJson = JsonConvert.SerializeObject(leaguesWithTeams, Formatting.Indented); File.WriteAllText("../../leagues-and-teams.json", leaguesWithTeamsJson); }
public static void Main() { var context = new FootballEntities(); int count = 0; var xmlDocument = XDocument.Load("../../leagues-and-teams.xml"); var leagueNodes = xmlDocument.XPathSelectElements("/leagues-and-teams/league"); foreach (var leagueNode in leagueNodes) { Console.WriteLine("Processing league #{0} ...", ++count); League league = null; string leagueName = null; if (leagueNode.Element("league-name") != null) { leagueName = leagueNode.Element("league-name").Value; league = context.Leagues.Where(l => l.LeagueName == leagueName).FirstOrDefault(); if (league == null) { league = new League() { LeagueName = leagueName }; context.Leagues.Add(league); context.SaveChanges(); Console.WriteLine("Created league: {0}", leagueName); } else { Console.WriteLine("Existing league: {0}", leagueName); } } var matchNodes = leagueNode.XPathSelectElements("teams/team"); foreach (var matchNode in matchNodes) { string teamName = matchNode.Attribute("name").Value; string teamCountry = null; if (matchNode.Attribute("country") != null) { teamCountry = matchNode.Attribute("country").Value; } var team = context.Teams.Where(t => t.TeamName == teamName).FirstOrDefault(); if (team == null) { team = new Team() { TeamName = teamName, CountryCode = context.Countries.Where(c => c.CountryName == teamCountry).Select(c => c.CountryCode).FirstOrDefault() }; context.Teams.Add(team); context.SaveChanges(); Console.WriteLine("Created team: {0} ({1})", teamName, teamCountry); } else { Console.WriteLine("Existing team: {0} ({1})", teamName, teamCountry); } if (leagueName != null) { if (league.Teams.Contains(team)) { Console.WriteLine("Existing team in league: {0} belongs to {1}", leagueName, teamName); } else { team.Leagues.Add(league); context.SaveChanges(); Console.WriteLine("Added team to league: {0} to league {1}", leagueName, teamName); } } } } }
static void Main() { var footballEntities = new FootballEntities(); var internationalMatches = footballEntities.InternationalMatches .OrderBy(im => im.MatchDate) .ThenBy(im => im.HomeCountry.CountryName) .ThenBy(im => im.AwayCountry.CountryName) .Select(im => new { homeCountryCode = im.HomeCountryCode, homeCountryName = im.HomeCountry.CountryName, awayCountryCode = im.AwayCountryCode, awayCountryName = im.AwayCountry.CountryName, dateTime = im.MatchDate, homeScore = im.HomeGoals, awayScore = im.AwayGoals, leagueName = im.League.LeagueName }) .ToList(); var document = new XDocument(new XDeclaration("1.0", "UTF-8", "yes")); XElement matches = new XElement("matches"); foreach (var internationalMatch in internationalMatches) { var match = new XElement("match"); if (internationalMatch.dateTime != null) { if (internationalMatch.dateTime.Value.TimeOfDay.TotalMilliseconds == 0) { var dateAttribute = new XAttribute("date", internationalMatch.dateTime.Value.ToString("dd-MM-yyyy")); match.Add(dateAttribute); } else { var datetimeAttribute = new XAttribute("datetime", internationalMatch.dateTime.Value.ToString("dd-MM-yyyy HH:mm:ff")); match.Add(datetimeAttribute); } } var homeCountry = new XElement("home-country"); homeCountry.Add(new XAttribute("code", internationalMatch.homeCountryCode)); homeCountry.Value = internationalMatch.homeCountryName; match.Add(homeCountry); var awayCountry = new XElement("away-country"); awayCountry.Add(new XAttribute("code", internationalMatch.awayCountryCode)); awayCountry.Value = internationalMatch.awayCountryName; match.Add(awayCountry); if (internationalMatch.homeScore != null && internationalMatch.awayScore != null) { var score = new XElement("score"); score.Value = string.Format("{0}:{1}", internationalMatch.homeScore, internationalMatch.awayScore); match.Add(score); } if (internationalMatch.leagueName != null) { var league = new XElement("league"); league.Value = string.Format("{0}", internationalMatch.leagueName); match.Add(league); } matches.Add(match); } document.Add(matches); document.Save("../../international-matches.xml"); }
static void Main() { var footballContext = new FootballEntities(); Random random = new Random(); int count = 0; List <Team> allTeams = footballContext.Teams.Include("Leagues").ToList(); List <League> allLeagues = footballContext.Leagues.ToList(); XDocument xmlDocument = XDocument.Load("../../generate-matches.xml"); IEnumerable <XElement> generateNodes = xmlDocument.XPathSelectElements("/generate-random-matches/generate"); foreach (XElement generateNode in generateNodes) { Console.WriteLine("\r\nProcessing request #{0} ...", ++count); int generateCount = 10; int maxGoals = 5; DateTime startDate = Convert.ToDateTime("1-Jan-2000"); DateTime endDate = Convert.ToDateTime("31-Dec-2015"); League league = null; string leagueName = null; int? leagueId = null; if (generateNode.Attribute("generate-count") != null) { generateCount = int.Parse(generateNode.Attribute("generate-count").Value); } if (generateNode.Attribute("max-goals") != null) { maxGoals = int.Parse(generateNode.Attribute("max-goals").Value); } if (generateNode.Element("league") != null) { leagueName = generateNode.Element("league").Value; league = allLeagues.Where(l => l.LeagueName == leagueName).FirstOrDefault(); if (league != null) { leagueId = league.Id; } } if (generateNode.Element("start-date") != null) { startDate = Convert.ToDateTime(generateNode.Element("start-date").Value); } if (generateNode.Element("end-date") != null) { endDate = Convert.ToDateTime(generateNode.Element("end-date").Value); } for (int i = 0; i < generateCount; i++) { TimeSpan timeSpan = endDate - startDate; TimeSpan newSpan = new TimeSpan(0, random.Next(0, (int)timeSpan.TotalMinutes), 0); DateTime currentDate = startDate + newSpan; var possibleTeams = allTeams; if (league != null) { possibleTeams = possibleTeams .Where(t => t.Leagues.Select(l => l.Id).Contains(league.Id)) .ToList(); } var homeTeam = possibleTeams[random.Next(possibleTeams.Count())]; var awayTeam = possibleTeams[random.Next(possibleTeams.Count())]; var homeGoals = random.Next(maxGoals); var awayGoals = random.Next(maxGoals); TeamMatch currentTeamMatch = new TeamMatch() { HomeTeamId = homeTeam.Id, AwayTeamId = awayTeam.Id, HomeGoals = homeGoals, AwayGoals = awayGoals, MatchDate = currentDate, LeagueId = leagueId }; footballContext.TeamMatches.Add(currentTeamMatch); Console.WriteLine("{0} {1}: - {2}: {3}-{4} ({5})", currentDate.ToString("dd-MMM-yyyy"), homeTeam.TeamName, awayTeam.TeamName, homeGoals, awayGoals, leagueName ?? "no league"); } } footballContext.SaveChanges(); }