private static void CreateTeamIfNotExist(IEnumerable<XElement> teamNodes, League league, FootballEntities context)
        {
            foreach (var teamNode in teamNodes)
            {
                string teamName = teamNode.Attribute("name").Value;
                var country = teamNode.Attribute("country");
                string countryName = null;
                if (country != null)
                {
                    countryName = country.Value;
                }

                var team = context.Teams
                    .Include(t => t.Leagues)
                    .FirstOrDefault(t => t.TeamName == teamName && t.Country.CountryName == countryName);
                if (team != null)
                {
                    Console.WriteLine("Existing team: {0} ({1})", team.TeamName, countryName ?? "no country");
                }
                else
                {
                    team = new Team()
                    {
                        TeamName = teamName,
                        Country = context.Countries.FirstOrDefault(c => c.CountryName == countryName)
                    };

                    context.Teams.Add(team);
                    context.SaveChanges();
                    Console.WriteLine("Created team: {0} ({1})", team.TeamName, countryName ?? "no country");
                }

                AddTeamToLeague(team, league, context);
            }
        }
        private static League CreateLeagueIfNotExist(XElement leagueNode, FootballEntities context)
        {
            League league = null;
            var leagueNameNode = leagueNode.Descendants("league-name").FirstOrDefault();
            if (leagueNameNode != null)
            {
                string leagueName = leagueNameNode.Value;
                league = context.Leagues.FirstOrDefault(l => l.LeagueName == leagueName);

                if (league == null)
                {
                    league = new League()
                    {
                        LeagueName = leagueName
                    };

                    context.Leagues.Add(league);
                    context.SaveChanges();

                    Console.WriteLine("Created league: {0}", league.LeagueName);
                }
                else
                {
                    Console.WriteLine("Existing league: {0}", leagueName);
                }
            }

            return league;
        }
 static void Main()
 {
     var context = new FootballEntities();
     foreach (var team in context.Teams)
     {
         Console.WriteLine(team.TeamName);
     }
 }
Exemple #4
0
        static void Main()
        {
            var context = new FootballEntities();

            foreach (var team in context.Teams)
            {
                Console.WriteLine(team.TeamName);
            }
        }
        static void Main()
        {
            var context = new FootballEntities();

            var allTeamNames = context.Teams
                .Select(t => t.TeamName);

            foreach (var teamName in allTeamNames)
            {
                Console.WriteLine(teamName);
            }
        }
        static void Main()
        {
            var context = new FootballEntities();

            var leaguesAndTeams = context.Leagues
                .OrderBy(l => l.LeagueName)
                .Select(l => new
                {
                    LeaugeName = l.LeagueName,
                    LeagueTeams = l.Teams.OrderBy(t => t.TeamName).Select(t => t.TeamName)
                });

            var json = new JavaScriptSerializer().Serialize(leaguesAndTeams);

            File.WriteAllText(@"leagues-and-teams.json", json);
        }
        public static void Main()
        {
            var context = new FootballEntities();

            var xmlDoc = XDocument.Load(@"..\..\leagues-and-teams.xml");
            var leagueNodes = xmlDoc.XPathSelectElements("/leagues-and-teams/league");
            int counter = 0;

            foreach (var leagueNode in leagueNodes)
            {
                Console.WriteLine("Processing league #{0} ...", ++counter);
                League league = CreateLeagueIfNotExist(leagueNode, context);
                var teamNodes = leagueNode.XPathSelectElements("teams/team");
                CreateTeamIfNotExist(teamNodes, league, context);
            }
        }
        private static void AddTeamToLeague(Team team, League league, FootballEntities context)
        {
            if (league != null)
            {
                if (!team.Leagues.Contains(league))
                {
                    team.Leagues.Add(league);
                    context.SaveChanges();

                    Console.WriteLine("Added team to league: {0} to league {1}", team.TeamName, league.LeagueName);
                }
                else
                {
                    Console.WriteLine("Existing team in league: {0} belongs to {1}", team.TeamName, league.LeagueName);
                }
            }
        }
        public static void Main()
        {
            var context = new FootballEntities();
            var leagues = context.Leagues
                .OrderBy(l => l.LeagueName)
                .Select(l => new
                {
                    Name = l.LeagueName,
                    Teams = l.Teams
                    .OrderBy(t => t.TeamName)
                    .Select(t => t.TeamName)
                });

            var jsSerializer = new JavaScriptSerializer();
            var jsonLeagues = jsSerializer.Serialize(leagues.ToList());

            File.WriteAllText(@"..\..\leagues-and-teams.json", jsonLeagues);
        }
        private static void CreateTeamsIfNotExists(
           FootballEntities context, IEnumerable<XElement> xTeams, League league)
        {
            foreach (var xTeam in xTeams)
            {
                // Find the team by team name and country name (if exists)
                var teamName = xTeam.Attribute("name").Value;
                var xCountry = xTeam.Attribute("country");
                string countryName = null;
                if (xCountry != null)
                {
                    countryName = xCountry.Value;
                }
                var team = context.Teams
                    .Include(t => t.Leagues)
                    .FirstOrDefault(
                        t => t.TeamName == teamName &&
                        t.Country.CountryName == countryName);

                // Create the team if it does not exists
                if (team != null)
                {
                    Console.WriteLine("Existing team: {0} ({1})",
                        team.TeamName, countryName ?? "no country");
                }
                else
                {
                    // Create a new team in the DB
                    team = new Team()
                    {
                        TeamName = teamName,
                        Country = context.Countries.FirstOrDefault(
                            c => c.CountryName == countryName),
                    };
                    context.Teams.Add(team);
                    context.SaveChanges();
                    Console.WriteLine("Created team: {0} ({1})",
                        team.TeamName, countryName ?? "no country");
                }

                AddTeamToLeague(context, team, league);
            }
        }
        static void Main()
        {
            var context = new FootballEntities();
            var leagues = context.Leagues;

            var leaguesQuery = leagues
                    .OrderBy(l => l.LeagueName)
                    .Select(l => new
                    {
                        leagueName = l.LeagueName,
                        teams = l.Teams
                                      .OrderBy(t => t.TeamName)
                                      .Select(t => t.TeamName)
                    });

            var jsSerializer = new JavaScriptSerializer();
            var leaguesJson = jsSerializer.Serialize(leaguesQuery);

            File.WriteAllText(@"../../leagues-and-teams.json", leaguesJson);
            Console.WriteLine(@"Leagues exported to leagues-and-teams.json");
        }
 private static League CreateLeagueIfNotExists(FootballEntities context, XElement xLeague)
 {
     League league = null;
     var xElementLeagueName = xLeague.Element("league-name");
     if (xElementLeagueName != null)
     {
         string leagueName = xElementLeagueName.Value;
         league = context.Leagues.FirstOrDefault(l => l.LeagueName == leagueName);
         if (league != null)
         {
             Console.WriteLine("Existing league: {0}", leagueName);
         }
         else
         {
             // Create a new league in the DB
             league = new League() { LeagueName = leagueName };
             context.Leagues.Add(league);
             context.SaveChanges();
             Console.WriteLine("Created league: {0}", leagueName);
         }
     }
     return league;
 }
        static void Main()
        {
            // Ensure date formatting will use the English names
            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

            var context = new FootballEntities();
            var matches = context.InternationalMatches;

            var matchesQuery = matches
                .OrderBy(m => m.MatchDate)
                .ThenBy(m => m.HomeCountry.CountryName)
                .ThenBy(m => m.AwayCountry.CountryName)
                .Select(m => new
                {
                    dateTime = m.MatchDate,
                    homeCountryName = m.HomeCountry.CountryName,
                    homeCountryCode = m.HomeCountryCode,
                    awayCountryName = m.AwayCountry.CountryName,
                    awayCountryCode = m.AwayCountryCode,
                    homeGoals = m.HomeGoals,
                    awayGoals = m.AwayGoals,
                    leagueName = m.League.LeagueName
                });

            // Build the output XML
            var xmlMatches = new XElement("matches");
            foreach (var match in matchesQuery)
            {
                var xmlMatch = new XElement("match");
                if (match.dateTime != null)
                {
                    if (match.dateTime.Value.TimeOfDay == TimeSpan.Zero)
                    {
                        string date = match.dateTime.Value.ToString("dd-MMM-yyyy");
                        xmlMatch.Add(new XAttribute("date-time", date));
                    }
                    else
                    {
                        string dateTime = match.dateTime.Value.ToString("dd-MMM-yyyy hh:mm");
                        xmlMatch.Add(new XAttribute("date-time", dateTime));
                    }
                }

                var xmlHomeCountry = new XElement("home-country", match.homeCountryName);
                xmlHomeCountry.Add(new XAttribute("code", match.homeCountryCode));
                xmlMatch.Add(xmlHomeCountry);

                var xmlAwayCountry = new XElement("away-country", match.awayCountryName);
                xmlAwayCountry.Add(new XAttribute("code", match.awayCountryCode));
                xmlMatch.Add(xmlAwayCountry);

                if (match.homeGoals != null && match.awayGoals != null)
                {
                    xmlMatch.Add(new XElement("score", match.homeGoals + "-" + match.awayGoals));
                }
                if (match.leagueName != null)
                {
                    xmlMatch.Add(new XElement("league", match.leagueName));
                }

                xmlMatches.Add(xmlMatch);
            }
            Console.WriteLine(xmlMatches);

            var xmlDoc = new XDocument(xmlMatches);
            xmlDoc.Save(@"..\..\international-matches.xml ");
        }
        private static void ProcessRequest(Request request)
        {
            var context = new FootballEntities();
            var teamsForMatchesQuery = context.Teams.AsQueryable();

            var league = context.Leagues.FirstOrDefault(l => l.LeagueName == request.LeagueName);
            if (request.LeagueName != null)
            {
                teamsForMatchesQuery = teamsForMatchesQuery.Where(
                    t => t.Leagues.Select(l => l.Id).Contains(league.Id));
            }
            var teams = teamsForMatchesQuery.ToList();

            var rnd = new Random();
            var diffDays = (request.EndDate - request.StartDate).Days;
            for (int i = 0; i < request.GenerateCount; i++)
            {
                var match = new TeamMatch();
                match.HomeTeam = teams[rnd.Next(teams.Count)];
                match.AwayTeam = teams[rnd.Next(teams.Count)];
                match.HomeGoals = rnd.Next(request.MaxGoals + 1);
                match.AwayGoals = rnd.Next(request.MaxGoals + 1);
                match.MatchDate = request.StartDate.AddDays(rnd.Next(diffDays + 1));
                match.League = league;

                context.TeamMatches.Add(match);
                context.SaveChanges();

                Console.WriteLine("{0}: {1} - {2}: {3}-{4} ({5})",
                    match.MatchDate.Value.ToString("dd-MMM-yyyy"),
                    match.HomeTeam.TeamName,
                    match.AwayTeam.TeamName,
                    match.HomeGoals,
                    match.AwayGoals,
                    match.League != null ? match.League.LeagueName : "no league");
            }
        }
 static void Main()
 {
     var inputXml = XDocument.Load("../../leagues-and-teams.xml");
     var xLeagues = inputXml.XPathSelectElements("/leagues-and-teams/league");
     var context = new FootballEntities();
     int leaguesCount = 0;
     foreach (var xLeague in xLeagues)
     {
         Console.WriteLine("Processing league #{0} ...", ++leaguesCount);
         League league = CreateLeagueIfNotExists(context, xLeague);
         var xTeams = xLeague.XPathSelectElements("teams/team");
         CreateTeamsIfNotExists(context, xTeams, league);
         Console.WriteLine();
     }
 }
        static void Main(string[] args)
        {
            var context = new FootballEntities();

            var internationalMatches = context.InternationalMatches
                .OrderBy(m => m.MatchDate)
                .ThenBy(m => m.CountryHome.CountryName)
                .ThenBy(m => m.CountryAway.CountryName)
                .Select(m => new
                {
                    MatchDate = m.MatchDate != null ? m.MatchDate : null,
                    HomeCountryCode = m.HomeCountryCode,
                    AwayCountryCode = m.AwayCountryCode,
                    HomeTeam = m.CountryHome.CountryName,
                    AwayTeam = m.CountryAway.CountryName,
                    Score = (m.HomeGoals != null && m.AwayGoals != null) ? (m.HomeGoals + "-" + m.AwayGoals) : null,
                    League = m.League != null ? m.League.LeagueName : null
                });

            var xmlDoc = new XDocument();
            var xmlRoot = new XElement("matches");
            xmlDoc.Add(xmlRoot);
            foreach (var match in internationalMatches)
            {
                var matchXmlElement = new XElement("match");

                if(match.MatchDate != null)
                {
                    DateTime matchDate = DateTime.Parse(match.MatchDate.ToString());
                    if (matchDate.Hour == 0)
                    {
                        string formattedDate = matchDate.ToString("dd-MMM-yyyy");
                        var matchDateAttribute = new XAttribute("date", formattedDate);
                        matchXmlElement.Add(matchDateAttribute);
                    }
                    else
                    {
                        string formattedDate = matchDate.ToString("dd-MMM-yyyy hh:mm");
                        var matchDateAttribute = new XAttribute("date-time", formattedDate);
                        matchXmlElement.Add(matchDateAttribute);
                    }
                }

                var homeCountryXmlElement = new XElement("home-country", match.HomeTeam);
                var homeCountryXmlElementAttirbute = new XAttribute("code", match.HomeCountryCode);
                homeCountryXmlElement.Add(homeCountryXmlElementAttirbute);
                matchXmlElement.Add(homeCountryXmlElement);

                var awayCountryXmlElement = new XElement("away-country", match.AwayTeam);
                var awayCountryXmlElementAttirbute = new XAttribute("code", match.AwayCountryCode);
                awayCountryXmlElement.Add(awayCountryXmlElementAttirbute);
                matchXmlElement.Add(awayCountryXmlElement);

                if(match.League != null)
                {
                    var leagueXmlElement = new XElement("league", match.League);
                    matchXmlElement.Add(leagueXmlElement);
                }

                if (match.Score != null)
                {
                    var scoreXmlElement = new XElement("score", match.Score);
                    matchXmlElement.Add(scoreXmlElement);
                }
                xmlRoot.Add(matchXmlElement);
            }

            xmlDoc.Save("international-matches.xml");
        }
        public static void Main()
        {
            var context = new FootballEntities();
            var matches = context.InternationalMatches
                .OrderBy(im => im.MatchDate)
                .ThenBy(im => im.CountryHome.CountryName)
                .ThenBy(im => im.CountryAway.CountryName)
                .Select(im => new InternationalMatch
                {
                    LeagueName = im.League.LeagueName,
                    HomeCountryName = im.CountryHome.CountryName,
                    AwayCountryName = im.CountryAway.CountryName,
                    HomeCountryCode = im.HomeCountryCode,
                    AwayCountryCode = im.AwayCountryCode,
                    HomeCountryGoals = im.HomeGoals,
                    AwayCountryGoals = im.AwayGoals,
                    MatchDate = im.MatchDate
                });

            var xmlMatches = new XElement("matches");
            foreach (var match in matches)
            {
                var xmlMatch = new XElement("match");
                if (match.MatchDate != null)
                {
                    var matchDate = DateTime.Parse(match.MatchDate.ToString());
                    XAttribute xmlDate;
                    if (matchDate.TimeOfDay.TotalSeconds == 0)
                    {
                        xmlDate = new XAttribute("date", matchDate.ToString("dd-MMM-yyyy"));
                    }
                    else
                    {
                        xmlDate = new XAttribute("date-time", matchDate.ToString("dd-MMM-yyyy hh:mm"));
                    }

                    xmlMatch.Add(xmlDate);
                }

                var xmlHomeCountry = new XElement("home-country", match.HomeCountryName);
                var xmlAwayCountry = new XElement("away-country", match.AwayCountryName);
                var xmlHomeCountryAttribute = new XAttribute("code", match.HomeCountryCode);
                var xmlAwayCountryAttribute = new XAttribute("code", match.AwayCountryCode);

                xmlHomeCountry.Add(xmlHomeCountryAttribute);
                xmlAwayCountry.Add(xmlAwayCountryAttribute);

                xmlMatch.Add(xmlHomeCountry);
                xmlMatch.Add(xmlAwayCountry);

                bool hasMatchScore = match.HomeCountryGoals != null;
                if (hasMatchScore)
                {
                    string matchScore = match.HomeCountryGoals + "-" + match.AwayCountryGoals;
                    var xmlScore = new XElement("score", matchScore);

                    xmlMatch.Add(xmlScore);
                }

                bool hasMatchLeague = match.LeagueName != null;
                if (hasMatchLeague)
                {
                    var xmlLeague = new XElement("league", match.LeagueName);

                    xmlMatch.Add(xmlLeague);
                }

                xmlMatches.Add(xmlMatch);
            }

            var xmlDoc = new XDocument(xmlMatches);
            xmlDoc.Save(@"..\..\international-matches.xml");
        }
        static void Main(string[] args)
        {
            var context = new FootballEntities();

            XDocument xmlDoc = XDocument.Load("../../leagues-and-teams.xml");

            var leaguesAndTeams =
                from league in xmlDoc.Descendants("league")
                select new
                {
                    LeagueName = league.Element("league-name") != null ? league.Element("league-name").Value : null,
                    Teams = league.Element("teams") != null ? league.Element("teams").Elements() : null
                };

            int countXmlLeagues = 0;

            foreach (var league in leaguesAndTeams)
            {
                League leageToDb = null;
                Console.WriteLine("Processing league #{0}", ++countXmlLeagues);
                if(league.LeagueName != null)
                {
                    leageToDb = context.Leagues.FirstOrDefault(l => l.LeagueName == league.LeagueName);
                    if (leageToDb != null)
                    {
                        Console.WriteLine("Existing league: {0}", league.LeagueName);
                    }
                    else
                    {
                        leageToDb = new League() { LeagueName = league.LeagueName };
                        context.Leagues.Add(leageToDb);
                        Console.WriteLine("Created league: {0}", league.LeagueName);
                    }
                }

                if(league.Teams != null)
                {
                    foreach (var team in league.Teams)
                    {
                        string teamName = team.Attribute("name").Value;
                        string countryName = "";
                        if (team.Attribute("country") != null)
                        {
                            countryName = team.Attribute("country").Value;
                        }

                        Team teamToDb = context.Teams.FirstOrDefault(t => t.TeamName == teamName && t.Country.CountryName == countryName);
                        if(teamToDb == null)
                        {
                            teamToDb = context.Teams.FirstOrDefault(t => t.TeamName == teamName);
                        }

                        if(teamToDb != null)
                        {
                            Console.WriteLine("Existing team: {0} ({1})",
                                teamName, !string.IsNullOrEmpty(countryName) ? countryName : "no country");
                        }
                        else
                        {
                            Country country = context.Countries.FirstOrDefault(c => c.CountryName == countryName);
                            if(!string.IsNullOrEmpty(countryName))
                            {
                                if(country == null)
                                {
                                    country = new Country()
                                    {
                                        CountryName = countryName
                                    };
                                }
                            }
                            teamToDb = new Team()
                                {
                                    TeamName = teamName,
                                    Country = country
                                };
                            context.Teams.Add(teamToDb);

                            Console.WriteLine("Created team: {0} ({1})",
                                teamName, !string.IsNullOrEmpty(countryName) ? countryName : "no country");
                        }

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

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