private static List<Team> createTeams(XElement league, FootballEntities context, League leagues)
        {
            //List<Team> teams = new List<Team>();

            //if (league.Attribute("name")!=null)
            //{
            //    teams.Add(new Team()
            //{
            //    Country = context. league.Attribute("name").Value;
            //});
            //}

            return teams;
        }
        private static League CreateLeage(XElement league, FootballEntities context)
        {
            var val = league.Element("league-name").Value;

            if (val == null)
            {
                throw new Exception();
            }

            return new League
            {
                LeagueName = league.Value
            };
        }
        static void Main()
        {
            var context = new FootballEntities();
            //exporting all leagues along with their teams Order the leagues and the teams in each league alphabetically.
            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 leagueToJSON = JsonConvert.SerializeObject(leagues,Formatting.Indented);

            File.WriteAllText(@"leagues-and-teams.json", leagueToJSON);

            Process.Start(@"leagues-and-teams.json");
        }
        static void Main()
        {
            var context = new FootballEntities();

            var document = XDocument.Load(@"../../leagues-and-teams.xml");
            var leagueTag = document.XPathSelectElements(@"/leagues-and-teams/league");

            var leagueList = new List<League>();

            foreach (var league in leagueTag)
            {
                try
                {
                    var leagues = CreateLeage(league, context);
                    leagueList.Add(leagues);
                    if (league.Element("teams") != null)
                    {
                        List<Team> teams = createTeams(league, context, leagues);
                        teams.ForEach(t => context.Teams.Add(t));
                    }
                }
                catch (Exception)
                {
                    throw;
                }

            }

            foreach (var league in leagueList)
            {
                if (context.Leagues.Any(l => l.LeagueName != league.LeagueName))
                {
                    context.Leagues.AddOrUpdate(league);
                }
            }

            //context.SaveChanges();
        }
        static void Main(string[] args)
        {
            var context = new FootballEntities();
            //exporting all international matches and their Score
            var matches = context.InternationalMatches
                .Select(m => new
                {
                    HomeCountry = m.Country1.CountryName,
                    HomeCountryCode = m.Country1.CountryCode,
                    AwayCountry = m.Country.CountryName,
                    AwayCountryCode = m.Country.CurrencyCode,
                    League = m.League.LeagueName,
                    Date = m.MatchDate,
                    Score = m.HomeGoals+ "-"+ m.AwayGoals
                })
                .OrderBy(m=>m.Date)
                .ThenBy(m=>m.AwayCountry)
                .ThenBy(m=>m.HomeCountry);
            //Order the matches by date (from the earliest) and by home country and away country alphabetically as second and third criteria.
            var document = new XDocument();

            XElement matchesTag = new XElement("matches");

              //          <match date-time="26-Jun-1994 11:35">
              //            <home-country code="BG">Bulgaria</home-country>
              //            <away-country code="GR">Greece</away-country>
              //            <Score>4-0</Score>
              //            </match>

            foreach (var match in matches)
            {
                XElement matchTag = new XElement("match");

                var homeTag = new XElement("home-country",match.HomeCountry);
                homeTag.Add(new XAttribute("code", match.HomeCountryCode));
                matchTag.Add(homeTag);

                var awayTag = new XElement("away-country", match.AwayCountry);
                awayTag.Add(new XAttribute("code", match.AwayCountryCode));
                matchTag.Add(awayTag);

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

                if (match.Score != "-")
                {
                    matchTag.Add(new XAttribute("date-time", match.Date.Value.ToString("dd MMMM, yyyy H:mm")));

                    var scoreTag = new XElement("score", match.Score);
                    matchTag.Add(scoreTag);
                }
                matchesTag.Add(matchTag);
            }

            document.Add(matchesTag);
            document.Save(@"international-matches.xml");

            System.Diagnostics.Process.Start(@"international-matches.xml");
        }
Exemplo n.º 6
0
        static void Main()
        {
            using (var db = new FootballEntities())
            {
                // Prboel 01
                foreach (var team in db.Teams)
                {
                    Console.WriteLine(team.TeamName);
                }

                // Poblem 02
                var leaguesQuery = db.Leagues.OrderBy(l => l.LeagueName).Select(l => new
                {
                    leagueName = l.LeagueName,
                    teams = l.Teams.OrderBy(t => t.TeamName).Select(t => t.TeamName)
                }).ToList();

                var serializer = new JavaScriptSerializer();
                var leaguesToJson = serializer.Serialize(leaguesQuery);

                File.WriteAllText("../../leagues-and-teams.json", leaguesToJson);

                // Problem 03
                var internationalMatches = db.InternationalMatches
                    .OrderBy(m => m.MatchDate)
                    .ThenBy(m => m.HomeCountry.CountryName)
                    .ThenBy(m => m.AwayCountry.CountryName)
                    .Select(m => new
                    {
                        countryCodeHomeTeam = m.HomeCountryCode,
                        countryCodeAwayTeam = m.AwayCountryCode,
                        countryNameHomeTeam = m.HomeCountry.CountryName,
                        countryNameAwayTeam = m.AwayCountry.CountryName,
                        matchPlayedOnDate = m.MatchDate,
                        matchLeagueName = m.League.LeagueName,
                        matchHomeTeamScore = m.HomeGoals,
                        matchAwayTeamScore = m.AwayGoals
                    });

                var allMatches = new XElement("matches");

                foreach (var internationalMatch in internationalMatches)
                {
                    // CURRENT MATCH
                    var currentMatch = new XElement
                    (
                        "match",
                        new XElement
                        (
                            "home-country",
                            new XAttribute("code", internationalMatch.countryCodeHomeTeam), internationalMatch.countryNameHomeTeam
                        ),
                        new XElement
                        (
                            "away-country",
                            new XAttribute("code", internationalMatch.countryCodeAwayTeam), internationalMatch.countryNameAwayTeam
                        )
                    );

                    // CURRENT MATCH LEAGUE NAME
                    if (internationalMatch.matchLeagueName != null)
                    {
                        currentMatch.Add(new XElement("league", internationalMatch.matchLeagueName));
                    }

                    // CURRENT MATCH SCORES
                    if (internationalMatch.matchHomeTeamScore != null && internationalMatch.matchAwayTeamScore != null)
                    {
                        currentMatch.Add
                        (
                            new XElement
                            (
                                "score",
                                string.Format("{0}-{1}", internationalMatch.matchHomeTeamScore, internationalMatch.matchAwayTeamScore)
                            )
                        );
                    }

                    // CURRENT MATCH PLAYED DATE
                    if (internationalMatch.matchPlayedOnDate != null)
                    {
                        DateTime dateTime;
                        DateTime.TryParse(internationalMatch.matchPlayedOnDate.ToString(), out dateTime);

                        var dateFormat = dateTime.TimeOfDay.TotalSeconds == 0.0D ? "date" : "date-time";
                        var matchDate = dateFormat == "date-time"
                            ? string.Format("{0:dd-MMM-yyyy hh:mm}", internationalMatch.matchPlayedOnDate)
                            : string.Format("{0:dd-MMM-yyyy}", internationalMatch.matchPlayedOnDate);

                        currentMatch.Add(new XAttribute(dateFormat, matchDate));
                    }

                    // PUT CURRENT MATCH TO ALL MATCHES
                    allMatches.Add(currentMatch);
                }

                // OUTPUT ALL MATCHES TO XML FILE
                allMatches.Save("../../international-matches.xml");

                // Problem 04
                var xmlDocument = XDocument.Load("../../leagues-and-teams.xml");
                var leagues = xmlDocument.Descendants("league");
                var processId = 1;

                foreach (var league in leagues)
                {

                    Console.WriteLine("Processing league #{0} ...", processId++);
                    var leagueName = league.Value;
                    var currentLeague = db.Leagues.FirstOrDefault(l => l.LeagueName == leagueName);

                    if (currentLeague != null)
                    {
                        // LEAGUE ALREADY EXISTS
                        Console.WriteLine("Existing league: {0}", leagueName);
                    }
                    else if (!string.IsNullOrWhiteSpace(leagueName))
                    {
                        currentLeague = new League()
                        {
                            LeagueName = leagueName
                        };

                        db.Leagues.Add(currentLeague);

                        db.SaveChanges();

                        // LEAGUE CREATED
                        Console.WriteLine("Created league: {0}", leagueName);
                    }

                    foreach (var team in league.Descendants("team"))
                    {
                        var teamName = team.Attribute("name").Value;
                        var countryName = team.Attribute("country")?.Value;
                        var currentTeam = db.Teams
                            .FirstOrDefault(t => t.TeamName == teamName && t.Country.CountryName == countryName);

                        if (currentTeam != null)
                        {
                            // TEAM ALREADY EXIST
                            Console.WriteLine("Existing team: {0} ({1})", teamName, countryName ?? "no cuntry");
                        }
                        else
                        {
                            // CREATE CURRENT TEAM
                            currentTeam = new Team()
                            {
                                TeamName = teamName,
                                Country = db.Countries.FirstOrDefault(c => c.CountryName == countryName)
                            };

                            // ADD CURRENT TEAM TO TEAMS
                            db.Teams.Add(currentTeam);

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

                            if (currentLeague != null)
                            {
                                if (currentLeague.Teams.Contains(currentTeam))
                                {
                                    // CURRENT TEAM EXIST IN CURRENT LEAGUE
                                    Console.WriteLine("Existing team in league: {0} belongs to {1}", teamName, leagueName);
                                }
                                else
                                {
                                    // ADDED CURRENT TEAM TO CURRENT LEAGUE
                                    currentLeague.Teams.Add(currentTeam);
                                    Console.WriteLine("Added team to league: {0} to league {1}", teamName, leagueName);
                                }
                            }

                            db.SaveChanges();
                        }
                    }
                }

                // Problem 5
                var document = XDocument.Load("../../generate-matches.xml");

                //Console.WriteLine(document);

                var generateData = document.Descendants("generate");

                // IF GENERATE DATA EXIST
                var generateEntries = generateData as XElement[] ?? generateData.ToArray();
                if (generateEntries.Any())
                {
                    var rand = new Random();
                    var requestId = 1;

                    foreach (var generateEntry in generateEntries)
                    {
                        Console.WriteLine("Processing request #{0} ...", requestId++);

                        var generateCount = int.Parse(generateEntry.Attribute("generate-count")?.Value ?? "10");
                        var maxGoals = int.Parse(generateEntry.Attribute("max-goals")?.Value ?? "5");
                        var leagueName = generateEntry.XPathSelectElement("league")?.Value ?? "no league";
                        var startDate = DateTime.Parse(generateEntry.XPathSelectElement("start-date")?.Value ?? "1-Jan-2000");
                        var endDate = DateTime.Parse(generateEntry.XPathSelectElement("end-date")?.Value ?? "31-Dec-2015");

                        int startDateYear = startDate.Year,
                            startDateMonth = startDate.Month,
                            startDateDay = startDate.Day,
                            endDateYear = endDate.Year,
                            endDateMonth = endDate.Month,
                            endDateDay = endDate.Day;

                        for (var i = 0; i < generateCount; i++)
                        {
                            int randomDateYear = rand.Next(startDateYear, endDateYear + 1),
                                randomDateMonth = rand.Next(startDateMonth, endDateMonth + 1),
                                randomDateDay = rand.Next(startDateDay, endDateDay);

                            // RANDOM TEAMS
                            string randomHomeTeam,
                                   randomAwayTeam;

                            var leagueTeams = db.Leagues.FirstOrDefault(l => l.LeagueName == leagueName)
                                   ?.Teams.Select(t => t.TeamName).ToList();

                            if (leagueTeams != null)
                            {
                                int randomHomeTeamId, randomAwayTeamId;

                                while (true)
                                {
                                    randomHomeTeamId = rand.Next(0, leagueTeams.Count);
                                    randomAwayTeamId = rand.Next(0, leagueTeams.Count);

                                    if (randomHomeTeamId != randomAwayTeamId) break;
                                }

                                randomHomeTeam = leagueTeams[randomHomeTeamId];
                                randomAwayTeam = leagueTeams[randomAwayTeamId];
                            }
                            else
                            {
                                var allTeams = db.Teams.Select(t => t.TeamName).ToList();

                                int randomHomeTeamId, randomAwayTeamId;

                                while (true)
                                {
                                    randomHomeTeamId = rand.Next(0, allTeams.Count);
                                    randomAwayTeamId = rand.Next(0, allTeams.Count);

                                    if (randomHomeTeamId != randomAwayTeamId) break;
                                }

                                randomHomeTeam = allTeams[randomHomeTeamId];
                                randomAwayTeam = allTeams[randomAwayTeamId];
                            }

                            // RANDOM DATE
                            DateTime randomDate = new DateTime(randomDateYear, randomDateMonth, randomDateDay);
                            var randomDateString = string.Format("{0:dd-MMM-yyyy}", randomDate);

                            // RANDOM SCORES
                            var homeTeamScore = rand.Next(0, maxGoals);
                            var awayTeamScore = rand.Next(0, maxGoals);

                            db.TeamMatches.Add(new TeamMatch()
                            {
                                HomeTeam = db.Teams.FirstOrDefault(t => t.TeamName == randomHomeTeam),
                                HomeTeamId = db.Teams.FirstOrDefault(t => t.TeamName == randomHomeTeam)?.Id ?? 0,
                                AwayTeam = db.Teams.FirstOrDefault(t => t.TeamName == randomAwayTeam),
                                AwayTeamId = db.Teams.FirstOrDefault(t => t.TeamName == randomAwayTeam)?.Id ?? 0,
                                HomeGoals = homeTeamScore,
                                AwayGoals = awayTeamScore,
                                MatchDate = randomDate,
                                League = db.Leagues.FirstOrDefault(l => l.LeagueName == leagueName),
                                LeagueId = db.Leagues.FirstOrDefault(l => l.LeagueName == leagueName)?.Id
                            });

                            db.SaveChanges();

                            // RESULT
                            Console.WriteLine("{0}: {1} - {2}: {3}-{4} ({5})",
                                randomDateString,
                                randomHomeTeam, randomAwayTeam,
                                homeTeamScore, awayTeamScore,
                                leagueName);
                        }
                        Console.WriteLine();

                    }
                }
            }
        }
        static void Main()
        {
            var context = new FootballEntities();

            var document = XDocument.Load(@"../../generate-matches.xml");
            var generatorTag = document.XPathSelectElements(@"/generate-random-matches");

            int counter = 0;

            foreach (var tag in generatorTag.Descendants("generate"))
            {

                string league = "no league";
                int count = 10;
                int maxGoals = 5;
                DateTime start = DateTime.Parse("1-Jan-2000");
                DateTime end = DateTime.Parse("31-Dec-2015");

                if (tag.Element("league") != null)
                {
                    league = tag.Element("league").Value;
                }

                if (tag.Attribute("generate-count") != null)
                {
                    count = int.Parse(tag.Attribute("generate-count").Value);
                }

                if (tag.Attribute("max-goals") != null)
                {
                    maxGoals = int.Parse(tag.Attribute("max-goals").Value);
                }

                if (tag.Element("start-date") != null)
                {
                    start = DateTime.Parse(tag.Element("start-date").Value);
                }

                if (tag.Element("end-date") != null)
                {
                    end = DateTime.Parse(tag.Element("end-date").Value);
                }

                var randGen = new Random();

                counter++;
                Console.WriteLine("Processing request #{0} ...",counter);
                for (int i = 0; i < count; i++)
                {
                    var teamcount = randGen.Next(1, context.Teams.Count());
                    var teamcount1 = randGen.Next(1, context.Teams.Count());

                    var randomTeam = context.Teams.Where(t => t.Id == teamcount).FirstOrDefault().TeamName;

                    var randomTeam1 = context.Teams.Where(t => t.Id == teamcount1).FirstOrDefault().TeamName;

                    Console.WriteLine("{0}: {1} - {2}: {3}-{4} ({5})", GetRandomDate(start, end).ToString("D"), randomTeam, randomTeam1, randGen.Next(0, maxGoals), randGen.Next(0, maxGoals), league);
                }
                Console.WriteLine();
            }
        }