static void Main()
 {
     var context = new FootballEntities();
     var teams = context.Teams.Select(t => t.TeamName);
     foreach (var team in teams)
     {
         Console.WriteLine(team);
     }
 }
        static void Main()
        {
            var context = new FootballEntities();
            var result = context.InternationalMatches.OrderBy(im => im.MatchDate)
                .ThenBy(im => im.CountryHome.CountryName)
                .ThenBy(im => im.CountryAway.CountryName).
                Select(im => new
                {
                     im.HomeCountryCode,
                     im.AwayCountryCode,
                     im.HomeGoals,
                     im.AwayGoals,
                     im.MatchDate,
                     HomeCountry = im.CountryHome.CountryName,
                     AwayCountry = im.CountryAway.CountryName,
                     League = im.League.LeagueName
                }).ToList();

            XElement matches = new XElement("matches");
            foreach (var match in result)
            {
                XElement Xmatches = new XElement("match",
                    new XElement("home-country",
                    new XAttribute("code", match.HomeCountryCode), match.HomeCountry),
                    new XElement("away-country",
                    new XAttribute("code", match.AwayCountryCode), match.AwayCountry));

                if (match.League!=null)
                {
                    Xmatches.Add(new XElement("league", match.League));
                }
                if (match.HomeGoals != null)
                {
                    Xmatches.Add(new XElement("score", match.HomeGoals + "-" +match.AwayGoals));
                }
                if (match.MatchDate != null)
                {
                    DateTime dt = match.MatchDate.Value;
                    if (dt.TimeOfDay == TimeSpan.Zero)
                    {
                        Xmatches.Add(new XAttribute("date", dt.ToString("dd-MMM-yyyy")));
                    }
                    else
                    {
                        Xmatches.Add(new XAttribute("date-time", dt.ToString("dd-MMM-yyyy hh:mm")));
                    }
                }

                matches.Add(Xmatches);
            }
            matches.Save("../../international-matches.xml");
        }
        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)
            }).ToList();

            var ser = new JavaScriptSerializer();
            var json = ser.Serialize(leagues);
            System.IO.File.WriteAllText("../../leagues-and-teams.json", json);

            //foreach (var team in teams)
            //{
            //    Console.WriteLine("League: " + team.LeagueName + " ");
            //    foreach (var t in team.Teams)
            //    {
            //        Console.WriteLine(t.TeamName);
            //    }
            //}
        }
        static void Main()
        {
            var context = new FootballEntities();
            var doc = XDocument.Load(@"..\\..\\leagues-and-teams.xml");
            var leagueNodes = doc.XPathSelectElements("/leagues-and-teams/league");
            int index = 1;
            foreach (var leagueNode in leagueNodes)
            {
                Console.WriteLine("Processing league #{0} ...", index++);
                League league = null;
                if (leagueNode.Element("league-name") != null)
                {
                    string leagueName = leagueNode.Element("league-name").Value;
                    if (context.Leagues.Any(l => l.LeagueName == leagueName))
                    {
                        Console.WriteLine("Existing league: {0}", leagueName);
                        league = context.Leagues.FirstOrDefault(l => l.LeagueName == leagueName);
                    }
                    else
                    {
                        league = (new League()
                        {
                            LeagueName = leagueName
                        });
                        context.Leagues.Add(league);
                        Console.WriteLine("Creating league: {0}", leagueName);
                    }
                }
                var teamNodes = leagueNode.XPathSelectElements("teams/team");
                if (teamNodes.Count() != 0)
                {
                    foreach (var teamNode in teamNodes)
                    {
                        var teamNames = teamNode.Attributes("name").Select(t => t.Value);
                        string teamName = teamNames.FirstOrDefault().ToString();
                        string countryName = "";
                        string code = null;
                        if (teamNode.Attributes("country").Any())
                        {
                            var countryNames = teamNode.Attributes("country").Select(t => t.Value);
                            countryName = countryNames.FirstOrDefault().ToString();
                            var codes =
                                context.Countries.Where(c => c.CountryName == countryName).Select(c => c.CountryCode);
                            code = codes.FirstOrDefault().ToString();
                        }
                        Team team = null;
                        var query =
                            context.Teams.Select(t => t.TeamName == teamName && t.Country.CountryName == countryName);
                        if (query.First()!=false)
                        {
                            Console.WriteLine("Existing team: {0}", teamName);
                            team = context.Teams.FirstOrDefault(l => l.TeamName == teamName);
                            }
                        else
                        {
                            if (code != null)
                            {
                                team = new Team()
                                {
                                    TeamName = teamName,
                                    CountryCode = code
                                };
                            }
                            else
                            {
                                team = new Team()
                                {
                                    TeamName = teamName
                                };
                            }

                            Console.WriteLine("Creating team: {0}", teamName);
                            context.Teams.Add(team);
                        }
                        if (league!=null && team!=null)
                        {
                            if (league.Teams.Contains(team))
                            {
                                Console.WriteLine("Existing team in league: {0} belongs to {1}",
                                    team.TeamName, league.LeagueName);
                            }
                            else
                            {
                                league.Teams.Add(team);
                                Console.WriteLine("Added team to league: {0} to league {1}",
                                    team.TeamName, league.LeagueName);
                            }
                        }

                    }
                }
            }
            context.SaveChanges();
        }
 static void Main()
 {
     var context = new FootballEntities();
     var doc = XDocument.Load(@"..\\..\\generate-matches.xml");
     var generateNodes = doc.XPathSelectElements("/generate-random-matches/generate");
     var index = 1;
     foreach (var generateNode in generateNodes)
     {
         int count = 10;
         int maxGoals = 5;
         int lId = 0;
         string league = null;
         DateTime startDate = new DateTime(2000, 1, 1);
         DateTime endDate = new DateTime(2015, 12, 31);
         Console.WriteLine("Processing request #{0} ...", index++);
         if (generateNode.Attributes("generate-count").Any())
         {
             var generateCounts = generateNode.Attributes("generate-count").Select(t => t.Value);
             count = int.Parse(generateCounts.FirstOrDefault().ToString());
         }
         if (generateNode.Attributes("max-goals").Any())
         {
             var goals = generateNode.Attributes("max-goals").Select(t => t.Value);
             maxGoals = int.Parse(goals.FirstOrDefault().ToString());
         }
         if (generateNode.Element("league") != null)
         {
             league = generateNode.Element("league").Value;
             var leagueId = context.Leagues.Where(l => l.LeagueName == league).Select(l => l.Id).First();
             lId = int.Parse(leagueId.ToString());
         }
         if (generateNode.Element("start-date") != null)
         {
             string date = generateNode.Element("start-date").Value;
             startDate = Convert.ToDateTime(date);
         }
         if (generateNode.Element("end-date") != null)
         {
             string enddate = generateNode.Element("end-date").Value;
             endDate = Convert.ToDateTime(enddate);
         }
         for (int i = 0; i < count; i++)
         {
             TimeSpan timeSpan = endDate - startDate;
             var randomDate = new Random();
             TimeSpan newSpan = new TimeSpan(0, randomDate.Next(0, (int)timeSpan.TotalMinutes), 0);
             DateTime newMatchDate = startDate + newSpan;
             string homeTeam = null;
             string awayTeam = null;
             int homeTeamId = 0;
             int awayTeamId = 0;
             if (league != null)
             {
                 var leagueQuery = context.Leagues.Where(l => l.LeagueName == league)
                     .Select(l => l.Teams.Select(t => t.TeamName)).ToList();
                 var list = leagueQuery[0].ToList();
                 while (awayTeam == homeTeam)
                 {
                     Random rnd = new Random();
                     int inx1 = rnd.Next(list.Count());
                     homeTeam = list[inx1];
                     int inx2 = rnd.Next(list.Count());
                     awayTeam = list[inx2];
                 }
                 var homeId = context.Teams.Where(t => t.TeamName == homeTeam).Select(t => t.Id).First();
                 homeTeamId = int.Parse(homeId.ToString());
                 var awayId = context.Teams.Where(t => t.TeamName == awayTeam).Select(t => t.Id).First();
                 awayTeamId = int.Parse(awayId.ToString());
             }
             else
             {
                 var teamQuery = context.Teams
                     .Select(t => t.TeamName);
                 var list = teamQuery.ToList();
                 while (awayTeam == homeTeam)
                 {
                     Random rnd = new Random();
                     int inx1 = rnd.Next(list.Count());
                     homeTeam = list[inx1];
                     int inx2 = rnd.Next(list.Count());
                     awayTeam = list[inx2];
                 }
                 var homeId = context.Teams.Where(t => t.TeamName == homeTeam).Select(t => t.Id).First();
                 homeTeamId = int.Parse(homeId.ToString());
                 var awayId = context.Teams.Where(t => t.TeamName == awayTeam).Select(t => t.Id).First();
                 awayTeamId = int.Parse(awayId.ToString());
             }
             Random rand = new Random();
             int homeTeamGoals = rand.Next(0, maxGoals);
             int awayTeamGoals = rand.Next(0, maxGoals);
             if (lId != 0)
             {
                 Console.WriteLine("{0}: {1} - {2}: {3}-{4} ({5})",
                     newMatchDate.ToString("dd/MMM/yyyy"),
                     homeTeam,awayTeam,homeTeamGoals,awayTeamGoals,league);
                 context.TeamMatches.Add(new TeamMatch()
                 {
                     HomeTeamId = homeTeamId,
                     AwayTeamId = awayTeamId,
                     HomeGoals = homeTeamGoals,
                     AwayGoals = awayTeamGoals,
                     MatchDate = newMatchDate,
                     LeagueId = lId
                 });
             }
             else
             {
                 Console.WriteLine("{0}: {1} - {2}: {3}-{4} (no leaugue)",
                     newMatchDate.ToString("dd/MMM/yyyy"),
                     homeTeam, awayTeam, homeTeamGoals, awayTeamGoals);
                 context.TeamMatches.Add(new TeamMatch()
                 {
                     HomeTeamId = homeTeamId,
                     AwayTeamId = awayTeamId,
                     HomeGoals = homeTeamGoals,
                     AwayGoals = awayTeamGoals,
                     MatchDate = newMatchDate
                     });
             }
             context.SaveChanges();
         }
     }
 }