static void Main() { var context = new FootballEntities(); var teamNames = context.Teams.Select(t => t.TeamName); foreach (var name in teamNames) { Console.WriteLine(name); } }
static void Main() { 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); }
static void Main() { var context = new FootballEntities(); XmlDocument doc = new XmlDocument(); doc.Load("../../leagues-and-teams.xml"); var root = doc.DocumentElement; int id = 1; foreach (XmlNode xmlLeague in root.ChildNodes) { Console.WriteLine("Processing league #{0} ...", id++); XmlNode leagueNameNode = xmlLeague.SelectSingleNode("league-name"); League league = null; if (leagueNameNode != null) { string leagueName = leagueNameNode.InnerText; league = context.Leagues.FirstOrDefault(l => l.LeagueName == leagueName); if (league != null) { Console.WriteLine("Existing league: {0}", leagueName); } else { league = new League() { LeagueName = leagueName }; Console.WriteLine("Created league: {0}", leagueName); } } XmlNode teamsNode = xmlLeague.SelectSingleNode("teams"); if (teamsNode != null) { foreach (XmlNode xmlTeam in teamsNode.ChildNodes) { Team team = null; string teamName = xmlTeam.Attributes["name"].Value; string countryName = null; if (xmlTeam.Attributes["country"] != null) { countryName = xmlTeam.Attributes["country"].Value; } team = context.Teams.FirstOrDefault(t => t.TeamName == teamName && t.Country.CountryName == countryName); if (team != null) { Console.WriteLine("Existing team: {0} ({1})", teamName, countryName ?? "no country"); } else { Country country = context.Countries.FirstOrDefault(c => c.CountryName == countryName); team = new Team() { TeamName = teamName, Country = country }; context.Teams.Add(team); Console.WriteLine("Created team: {0} ({1})", teamName, countryName ?? "no country"); } if (league != null) { if (league.Teams.Contains(team)) { Console.WriteLine("Existing team in league:{0} belongs to {1}", teamName, league.LeagueName); } else { league.Teams.Add(team); Console.WriteLine("Added team to league: {0} to league {1}", teamName, league.LeagueName); } } } } context.SaveChanges(); } }
static void Main() { var context = new FootballEntities(); var internationalMatches = context.InternationalMatches .OrderBy(im => im.MatchDate) .ThenBy(im => im.CountryHome.CountryName) .ThenBy(im => im.CountryAway.CountryName) .Select(im => new { HomeScore = im.HomeGoals, AwayScore = im.AwayGoals, HomeCountryCode = im.HomeCountryCode, AwayCountryCode = im.AwayCountryCode, Date = im.MatchDate, HomeCountryName = im.CountryHome.CountryName, AwayCountryName = im.CountryAway.CountryName, LeagueName = im.League.LeagueName } ).ToList(); XElement matches = new XElement("matches"); foreach (var match in internationalMatches) { XElement xmlMatch = new XElement("match", new XElement("home-country", new XAttribute("code", match.HomeCountryCode), match.HomeCountryName ), new XElement("away-country", new XAttribute("code", match.AwayCountryCode), match.AwayCountryName ) ); if (match.LeagueName != null) { xmlMatch.Add(new XElement("league", match.LeagueName)); } if (match.HomeScore != null && match.AwayScore != null) { xmlMatch.Add(new XElement("score", match.HomeScore + "-" + match.AwayScore)); } if (match.Date != null) { DateTime date = new DateTime(); DateTime.TryParse(match.Date.ToString(), out date); if (date.Hour != 0) { xmlMatch.Add(new XAttribute("date-time", date.ToString("dd-MMM-yyyy hh:mm"))); } else { xmlMatch.Add(new XAttribute("date", date.ToString("dd-MMM-yyyy"))); } } matches.Add(xmlMatch); } matches.Save("../../international-matches.xml"); }