static void Main(string[] args) { var context = new FootballEntities(); var xmlDoc = XDocument.Load("../../leagues-and-teams.xml"); foreach (var leagueElement in xmlDoc.XPathSelectElements("/leagues-and-teams/league")) { if (leagueElement.Element("league-name") != null) { var leagueNameCheck = leagueElement.Element("league-name").Value; var leagueAvailable = context.Leagues .Where(l => l.LeagueName == leagueNameCheck) .Select(l => l.LeagueName).ToList(); if (leagueAvailable.Count == 0) { var leagueEntity = new League(); leagueEntity.LeagueName = leagueElement.Element("league-name").Value; context.Leagues.Add(leagueEntity); } } if (leagueElement.Element("teams") != null) { var teamName = leagueElement.Element("teams").Element("team").Attribute("name").Value; var teamCountry = leagueElement.Element("teams").Element("team").Attribute("country").Value; var teamExisting = context.Teams.Where(t => t.TeamName == teamName && t.Country.CountryName == teamCountry) .Select(t => t.TeamName) .ToList(); if (teamExisting.Count == 0) { foreach (var teamElement in leagueElement.XPathSelectElements("teams/team")) { var teamEntity = new Team(); teamEntity.TeamName = teamElement.Attribute("name").Value; if (teamElement.Attribute("country") != null) { var country = teamElement.Attribute("country").Value; var countryCode = context.Countries .Where(c => c.CountryName == country) .Select(c => c.CountryCode).ToList(); teamEntity.CountryCode = countryCode[0]; } context.Teams.Add(teamEntity); } } } } context.SaveChanges(); }
static void Main(string[] args) { var context = new FootballEntities(); foreach (var team in context.Teams) { Console.WriteLine(team.TeamName); } }
static void Main(string[] args) { var context = new FootballEntities(); var exportQuery = 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(exportQuery); //Console.WriteLine(json); File.WriteAllText("leaguesAndTeams.json", json); }
static void Main(string[] args) { var context = new FootballEntities(); var intMatchesQueary = context.InternationalMatches.OrderBy(m => m.MatchDate) .ThenBy(m => m.HomeCountry) .ThenBy(m => m.AwayCountry) .Select(m => new { homeCountry = m.HomeCountry.CountryName, homeCode = m.HomeCountryCode, awayCountry = m.AwayCountry.CountryName, awayCode = m.AwayCountryCode, league = m.League.LeagueName, date = m.MatchDate, homeGoals = m.HomeGoals, awayGoals = m.AwayGoals }); var xmlDoc = new XDocument(); var xmlRoot = new XElement("matches"); xmlDoc.Add(xmlRoot); foreach (var match in intMatchesQueary) { XElement matchXml; if (match.date != null) { if (match.date.Value.TimeOfDay == TimeSpan.Zero) { matchXml = new XElement("match", new XAttribute("date-time", match.date.Value.ToString("dd-MMM-yyyy"))); xmlRoot.Add(matchXml); } else { matchXml = new XElement("match", new XAttribute("date", match.date.Value.ToString("dd-MMM-yyyy") + " " + match.date.Value.ToString("hh:mm"))); xmlRoot.Add(matchXml); } } else { matchXml = new XElement("match"); xmlRoot.Add(matchXml); } var homeCountryXml = new XElement("home-country", match.homeCountry, new XAttribute("code", match.homeCode)); matchXml.Add(homeCountryXml); var awayCountryXml = new XElement("away-country", match.awayCountry, new XAttribute("code", match.awayCode)); matchXml.Add(awayCountryXml); if (match.league != null) { var leagueXml = new XElement("league", match.league); matchXml.Add(leagueXml); } if(match.homeGoals != null) { var scoreXml = new XElement("score", match.homeGoals + "-"+ match.awayGoals); matchXml.Add(scoreXml); } } Console.WriteLine(xmlDoc); xmlDoc.Save("matches.xml"); }