static void Main()
        {
            using (var context = new FootballEntities())
            {
                var leagues = context.Leagues
                    .Select(l => new
                    {
                        leagueName = l.LeagueName,
                        teams = l.Teams
                            .Select(t => t.TeamName)
                            .OrderBy(t => t)
                    })
                    .OrderBy(l => l.leagueName)
                    .ToList();

                //foreach (var league in leagues)
                //{
                //    Console.WriteLine("---------{0}", league.leagueName);

                //    foreach (var team in league.teams)
                //    {
                //        Console.WriteLine(team.TeamName);
                //    }
                //}

                var json = new JavaScriptSerializer().Serialize(leagues);
                File.WriteAllText("../../leagues-and-teams.json", json);
            }
        }
        static void Main()
        {
            using (var context = new FootballEntities())
            {
                var teamNames = context.Teams
                    .Select(t => t.TeamName);

                foreach (var teamName in teamNames)
                {
                    Console.WriteLine(teamName);
                }
            }
        }
        static void Main()
        {
            Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;

            using (var context = new FootballEntities())
            {
                var internationalMatches = context.InternationalMatches
                    .Select(im => new
                    {
                        HomeCountryName = im.CountryHome.CountryName,
                        AwayCountryName = im.CountryAway.CountryName,

                        HomeCode = im.HomeCountryCode,
                        AwayCode = im.AwayCountryCode,

                        HomeScore = im.HomeGoals,
                        AwayScore = im.AwayGoals,

                        MatchDate = im.MatchDate,

                        League = im.League.LeagueName
                    })
                    .OrderBy(im => im.MatchDate)
                    .ThenBy(im => im.HomeCountryName)
                    .ThenBy(im => im.AwayCountryName)
                    .ToList();

                XElement matches = new XElement("matches");

                foreach (var match in internationalMatches)
                {
                    XElement xmlMatch = new XElement("match",
                        new XElement("home-country",
                            new XAttribute("code", match.HomeCode),
                            match.HomeCountryName),
                        new XElement("away-country",
                            new XAttribute("code", match.AwayCode),
                            match.AwayCountryName)
                            );

                    if (match.League != null)
                    {
                        xmlMatch.Add(new XElement("league", match.League));
                    }
                    if (match.HomeScore != null && match.AwayScore != null)
                    {
                        xmlMatch.Add(new XElement("score", string.Format("{0}-{1}", match.HomeScore, match.AwayScore)));
                    }

                    if (match.MatchDate != null)
                    {
                        var dateHours = match.MatchDate.Value.Hour;
                        var dateMinutes = match.MatchDate.Value.Minute;
                        DateTime date = DateTime.Parse(match.MatchDate.ToString());

                        if (dateHours != 0 || dateMinutes != 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);
                }

                //Console.WriteLine(matches);

                matches.Save("../../international-matches.xml");
            }
        }
        static void Main()
        {
            using (var context = new FootballEntities())
            {
                XmlDocument doc = new XmlDocument();
                doc.Load("../../leagues-and-teams.xml");

                XmlElement root = doc.DocumentElement;

                int leagueCounter = 1;

                foreach (XmlNode xmlLeague in root.ChildNodes)
                {
                    Console.WriteLine("Processing league #{0} ...", leagueCounter);
                    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
                        {
                            context.Leagues.Add(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);
                                }

                            }

                        }
                    }

                    leagueCounter++;
                    context.SaveChanges();
                    Console.WriteLine();
                }

            }
        }