private static void AddTeamsIntoLeague(League league, IEnumerable<XElement> teamElements, FootballEntities context)
        {
            foreach (var teamElement in teamElements)
            {
                if (teamElement.Attribute("name") == null)
                {
                    throw new ArgumentException("Atribute \"name\" is required.");
                }

                var currTeamCountryName = teamElement.Attribute("country") != null
                    ? teamElement.Attribute("country").Value
                    : null;
                var teamName = teamElement.Attribute("name").Value;

                // If this team exist
                if (context.Teams.Any(t => t.TeamName == teamName && t.Country.CountryName == currTeamCountryName))
                {
                    var existingTeam =
                        context.Teams.FirstOrDefault(
                            t => t.TeamName == teamName && t.Country.CountryName == currTeamCountryName);

                    Console.WriteLine("Existing team: {0} ({1})", existingTeam.TeamName,
                        existingTeam.Country != null ? existingTeam.Country.CountryName : "no country");

                    // If this team is in this league
                    if (league.Teams.Any(t => t.TeamName == existingTeam.TeamName && t.Country == existingTeam.Country))
                    {
                        Console.WriteLine("Existing team in league: {0} belongs to {1}", existingTeam.TeamName, league.LeagueName);
                    }
                    else
                    {
                        league.Teams.Add(existingTeam);
                        context.SaveChanges();

                        Console.WriteLine("Added team to league: {0} to league {1}", existingTeam.TeamName,
                        league.LeagueName);
                    }
                }
                else
                {
                    var newTeam = new Team
                    {
                        TeamName = teamName,
                        Country = context.Countries.FirstOrDefault(c => c.CountryName == currTeamCountryName)
                    };

                    context.Teams.Add(newTeam);
                    context.SaveChanges();

                    Console.WriteLine("Created team: {0} ({1})", newTeam.TeamName,
                        newTeam.Country != null ? newTeam.Country.CountryName : "no country");

                    league.Teams.Add(newTeam);
                    context.SaveChanges();

                    Console.WriteLine("Added team to league: {0} to league {1}", newTeam.TeamName,
                        league.LeagueName);
                }
            }
        }
        private static League CreateLeagueIfNotExtists(FootballEntities context, XElement xmlLeague)
        {
            League league = null;
            var xmlLeagueName = xmlLeague.Element("league-name");
            if (xmlLeagueName != null)
            {
                string leagueName = xmlLeagueName.Value;
                var leagueInDb = context.Leagues.FirstOrDefault(l => l.LeagueName == leagueName);

                if (leagueInDb != null)
                {
                    Console.WriteLine("Existing league: " + leagueInDb.LeagueName);
                    league = leagueInDb;
                }
                else
                {
                    league = new League { LeagueName = leagueName };
                    context.Leagues.Add(league);
                    context.SaveChanges();
                    Console.WriteLine("Created league: " + league.LeagueName);
                }
            }

            return league;
        }
        public static void Main()
        {
            var footballContext = new FootballEntities();

            foreach (var team in footballContext.Teams)
            {
                Console.WriteLine("Team: {0}", team.TeamName);
            }
        }
Ejemplo n.º 4
0
        public static void Main()
        {
            var context = new FootballEntities();
            var teamNames = context.Teams.Select(t => t.TeamName);

            foreach (var teamName in teamNames)
            {
                Console.WriteLine(teamName);
            }
        }
        public static void Main()
        {
            var context = new FootballEntities();
            var leaguesWithTeams = context.Leagues
                .OrderBy(l => l.LeagueName)
                .Select(l => new
                {
                    leagueName = l.LeagueName,
                    teams = l.Teams
                        .OrderBy(t => t.TeamName)
                        .Select(t => t.TeamName)
                });

            string leaguesWithTeamsJson = JsonConvert.SerializeObject(leaguesWithTeams, Formatting.Indented);
            File.WriteAllText("../../leagues-and-teams.json", leaguesWithTeamsJson);
        }
 private static void AddTeamToLeagueIfNotExist(FootballEntities context, Team team, League league)
 {
     if (league != null)
     {
         bool teamInLeague = league.Teams.Contains(team);
         if (!teamInLeague)
         {
             league.Teams.Add(team);
             context.SaveChanges();
             Console.WriteLine("Added team to league: " + team.TeamName + " to league " + league.LeagueName);
         }
         else
         {
             Console.WriteLine("Existing team in league: " + team.TeamName + " belongs to " + league.LeagueName);
         }
     }
 }
Ejemplo n.º 7
0
        private static Team GetTeamEntity(FootballEntities context, string name, string country)
        {
            Team teamEntity = context.Teams.FirstOrDefault(t => t.TeamName == name && t.Country.CountryName == country);

            if (teamEntity == null)
            {
                Team newTeamEntity = new Team {TeamName = name, Country = context.Countries.FirstOrDefault(c => c.CountryName == country)};
                context.Teams.Add(newTeamEntity);
                context.SaveChanges();
                Console.WriteLine("Created team: {0} ({1})", name, country ?? "no country");
            }
            else
            {
                Console.WriteLine("Existing team: {0} ({1})", name, country ?? "no country");
            }

            return teamEntity;
        }
Ejemplo n.º 8
0
        private static League GetLeagueEntity(FootballEntities context, XElement leagueName)
        {
            League leagueEntity = context.Leagues.FirstOrDefault(l => l.LeagueName == leagueName.Value);

            if (leagueEntity == null)
            {
                leagueEntity = new League {LeagueName = leagueName.Value};
                context.Leagues.Add(leagueEntity);
                context.SaveChanges();
                Console.WriteLine("Created league: {0}", leagueName.Value);
            }
            else
            {
                Console.WriteLine("Existing league: {0}", leagueName.Value);
            }

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

            XDocument doc = XDocument.Load("../../leagues-and-teams.xml");
            var leagesElements = doc.XPathSelectElements("leagues-and-teams/league");
            int leaguesCount = 1;

            foreach (var xmlLeague in leagesElements)
            {
                Console.WriteLine("Processing league #" + leaguesCount + " ...");
                League league = CreateLeagueIfNotExtists(context, xmlLeague);
                var xmlTeams = xmlLeague.XPathSelectElements("teams/team");
                CreateTeamsIfNotExist(context, league, xmlTeams);
                leaguesCount++;
                Console.WriteLine();
            }
        }
        private static void AddLeague(XElement leagueElement)
        {
            Console.WriteLine("Processing league #" + counter++ + " ...");

            var context = new FootballEntities();

            var leagueName = leagueElement.Element("league-name") != null
                    ? leagueElement.Element("league-name").Value
                    : null;

            if (leagueName == null)
            {
                AddTeams(leagueElement);
            }
            else
            {
                var teamElements = leagueElement.XPathSelectElements("teams/team");

                // If league exist
                if (context.Leagues.Any(l => l.LeagueName == leagueName))
                {
                    var existingLeague = context.Leagues.FirstOrDefault(l => l.LeagueName == leagueName);
                    Console.WriteLine("Existing league: {0}", existingLeague.LeagueName);

                    AddTeamsIntoLeague(existingLeague, teamElements, context);
                    context.SaveChanges();
                }
                else
                {
                    var newLeague = new League
                    {
                        LeagueName = leagueName
                    };
                    Console.WriteLine("Created league: {0}", newLeague.LeagueName);

                    AddTeamsIntoLeague(newLeague, teamElements, context);
                    context.Leagues.Add(newLeague);
                    context.SaveChanges();
                }


            }
            Console.WriteLine();
        }
Ejemplo n.º 11
0
        public static void Main()
        {
            var context = new FootballEntities();

            var doc = XDocument.Load(ImportFilePath);
            var leagues = doc.Descendants("league");

            var i = 1;

            foreach (XElement league in leagues)
            {
               Console.WriteLine("Processing league #{0} ...", i);
                var leagueName = league.Element("league-name");
                League leagueEntity = leagueName != null ? GetLeagueEntity(context, leagueName) : null;

                foreach (XElement team in league.XPathSelectElements("teams/team"))
                {
                    var name = team.Attribute("name").Value;
                    var countryNode = team.Attribute("country");
                    var country = countryNode != null ? countryNode.Value : null;

                    Team teamEntity = GetTeamEntity(context, name, country);

                    if (leagueName != null)
                    {
                        var existingTeam = leagueEntity.Teams.FirstOrDefault(t => t == teamEntity);
                        if (existingTeam == null)
                        {
                            teamEntity.Leagues.Add(leagueEntity);
                            context.SaveChanges();
                            Console.WriteLine("Added team to league: {0} to league {1}", name, leagueName.Value);
                        }
                        else
                        {
                            Console.WriteLine("Existing team in league: {0} belongs to {1}", name, leagueName.Value);
                        }
                    }
                }

                i++;
                Console.WriteLine();
            }
        }
Ejemplo n.º 12
0
        public static void Main()
        {
            CheckExistingFolder(ExportPath);

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

            var json = JsonConvert.SerializeObject(leagues, Formatting.Indented);
            //var json = JsonConvert.SerializeObject(leagues); // Use this line for non-indented output

            File.WriteAllText(ExportPath + ExportFilename, json);
            Console.WriteLine("File path: {0}", Path.GetFullPath(ExportPath + ExportFilename));
        }
        static void Main(string[] args)
        {
            var context = new FootballEntities();

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

            var json = JsonConvert.SerializeObject(leaguesWithTeamsQuery.ToList(), Formatting.Indented);

            File.WriteAllText(OutputPath, json);
            Console.WriteLine("JSON report has been saved in : " + OutputPath);
        }
        private static void CreateTeamsIfNotExist(
            FootballEntities context, League league, IEnumerable<XElement> xmlTeams)
        {
            if (xmlTeams == null)
            {
                return;
            }

            foreach (var xmlTeam in xmlTeams)
            {
                string teamName = xmlTeam.Attribute("name").Value;
                var country = xmlTeam.Attribute("country");
                string countryName = null;

                if (country != null)
                {
                    countryName = country.Value;
                }

                var team = context.Teams
                        .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 newTeam = new Team();
                    newTeam.TeamName = teamName;
                    newTeam.Country = context.Countries.FirstOrDefault(c => c.CountryName == countryName);
                    context.Teams.Add(newTeam);
                    context.SaveChanges();
                    Console.WriteLine("Created team: {0} ({1})", teamName, countryName ?? "no country");
                    team = newTeam;
                }

                AddTeamToLeagueIfNotExist(context, team, league);
            }
        }
Ejemplo n.º 15
0
        public static void Main()
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

            var context = new FootballEntities();

            var internationalMatches = context.InternationalMatches
                .OrderBy(m => m.MatchDate)
                .ThenBy(m => m.HomeCountry.CountryName)
                .ThenBy(m => m.AwayCountry.CountryName)
                .Select(m => new InternationalMatch
                {
                    HomeCountryCode = m.HomeCountryCode,
                    HomeCountry = m.HomeCountry.CountryName,
                    HomeGoals = m.HomeGoals,
                    AwayCountryCode = m.AwayCountryCode,
                    AwayCountry = m.AwayCountry.CountryName,
                    AwayGoals = m.AwayGoals,
                    League = m.League.LeagueName,
                    MatchDate = m.MatchDate
                });

            //DateTime myDate;
            //if (DateTime.TryParseExact(inputString, "dd-MM-yyyy hh:mm:ss",
            //    CultureInfo.InvariantCulture, DateTimeStyles.None, out myDate))
            //{
            //    //String has Date and Time
            //}
            //else
            //{
            //    //String has only Date Portion    
            //}


            GenerateXmlWithMatches(internationalMatches);
        }
        static void Main(string[] args)
        {

            var footballContext = new FootballEntities();

            var matches = footballContext.InternationalMatches
                .Include(m => m.AwayCountry)
                .Include(m => m.HomeCountry)
                .Include(m => m.League)
                .OrderBy(m => m.MatchDate)
                .ThenBy(m => m.HomeCountry)
                .ThenBy(m => m.AwayCountry);

            var xmlDoc = new XDocument();
            var xmlRoot = new XElement("matches");
            xmlDoc.Add(xmlRoot);

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

                if (match.MatchDate != null)
                {
                    var date = ((DateTime) match.MatchDate);

                    if (date.Hour == 0)
                    {
                        matchXmlElement.SetAttributeValue("date", date.ToString("dd-MMM-yy"));
                    }
                    else
                    {
                        matchXmlElement.SetAttributeValue("date-time", date.ToString("dd-MMM-yyyy hh:mm"));
                    }
                    
                }

                var homeCountryXmlElement = new XElement("home-country", match.HomeCountry.CountryName);
                homeCountryXmlElement.SetAttributeValue("code", match.HomeCountryCode);
                matchXmlElement.Add(homeCountryXmlElement);

                var awayCountryXmlElement = new XElement("away-country", match.AwayCountry.CountryName);
                awayCountryXmlElement.SetAttributeValue("code", match.AwayCountryCode);
                matchXmlElement.Add(awayCountryXmlElement);

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

                if (match.AwayGoals != null && match.HomeGoals != null)
                {
                    string score = String.Format("{0} - {1}", match.HomeGoals, match.AwayGoals);
                    var scoreXmlElement = new XElement("score", score);
                    matchXmlElement.Add(scoreXmlElement);
                }

                xmlRoot.Add(matchXmlElement);
            }

            xmlDoc.Save(OutputPath);
            Console.WriteLine("XML report has been saved in : " + OutputPath);
        }
        private static void AddTeams(XElement leagueElement)
        {
            var context = new FootballEntities();

            foreach (var teamElement in leagueElement.XPathSelectElements("teams/team"))
            {
                if (teamElement.Attribute("name") == null)
                {
                    throw new ArgumentException("Team name is required.");
                }

                var currTeamCountryName = teamElement.Attribute("country") != null
                    ? teamElement.Attribute("country").Value
                    : null;
                var teamName = teamElement.Attribute("name").Value;

                if (context.Teams.Any(t => t.TeamName == teamName && t.Country.CountryName == currTeamCountryName))
                {
                    var existingTeam =
                        context.Teams.FirstOrDefault(
                            t => t.TeamName == teamName && t.Country.CountryName == currTeamCountryName);

                    Console.WriteLine("Existing team: {0} ({1})", existingTeam.TeamName,
                        existingTeam.Country != null ? existingTeam.Country.CountryName : "no country");
                }
                else
                {
                    var newTeam = new Team
                    {
                        Country = context.Countries.FirstOrDefault(c => c.CountryName == currTeamCountryName),
                        TeamName = teamName
                    };

                    context.Teams.Add(newTeam);
                    context.SaveChanges();
                    Console.WriteLine("Created team: {0} ({1})", newTeam.TeamName,
                        newTeam.Country != null ? newTeam.Country.CountryName : "no country");
                }
            }
        }
Ejemplo n.º 18
0
 public static void Main()
 {
     var context = new FootballEntities();
     context.Teams.Select(t => t.TeamName).ToList().ForEach(Console.WriteLine);
 }
        public static void Main()
        {
            var context = new FootballEntities();
            var internationalMatches = context.InternationalMatches
                .OrderBy(im => im.MatchDate)
                .ThenBy(im => im.HomeCountry.CountryName)
                .ThenBy(im => im.AwayCountry.CountryName)
                .Select(im => new
                {
                    matchDateTime = im.MatchDate,
                    homeCountryCode = im.HomeCountryCode,
                    homeCountryName = im.HomeCountry.CountryName,
                    awayCountryCode = im.AwayCountryCode,
                    awayCountryName = im.AwayCountry.CountryName,
                    league = im.League.LeagueName,
                    homeGoals = im.HomeGoals,
                    awayGoals = im.AwayGoals
                });

            XElement rootElement = new XElement("matches");
            foreach (var im in internationalMatches)
            {
                XElement newMatch = new XElement("match");
                if (im.matchDateTime != null)
                {
                    if (im.matchDateTime.Value.TimeOfDay.Ticks != 0)
                    {
                        newMatch.SetAttributeValue(
                            "date-time",
                            im.matchDateTime.Value.ToString("dd-MMM-yyyy hh:mm", CultureInfo.InvariantCulture));
                    }
                    else
                    {
                        newMatch.SetAttributeValue(
                            "date",
                            im.matchDateTime.Value.ToString("dd-MMM-yyyy", CultureInfo.InvariantCulture));
                    }
                }

                XElement homeCountry = new XElement("home-country", im.homeCountryName);
                homeCountry.SetAttributeValue("code", im.homeCountryCode);
                newMatch.Add(homeCountry);
                XElement awayCountry = new XElement("away-country", im.awayCountryName);
                awayCountry.SetAttributeValue("code", im.awayCountryCode);
                newMatch.Add(awayCountry);

                if (!string.IsNullOrEmpty(im.league))
                {
                    XElement league = new XElement("league", im.league);
                    newMatch.Add(league);
                }

                if (im.homeGoals != null && im.awayGoals != null)
                {
                    XElement score = new XElement("score", im.homeGoals + "-" + im.awayGoals);
                    newMatch.Add(score);
                }

                rootElement.Add(newMatch);
            }

            rootElement.Save("../../international-matches.xml");
        }