0

I am currently working on a project and have hit a snag.

I'm taking in an unformatted string[] of football teams and I'm trying to filter the data and return it in a more orgainised format.

I'm fine with most of it (splitting the string to get the relevant values and sorting the format) except i have created a Team object which holds most of this data and as i loop through the original string i create a new team object each time i see a team name. Then i check to see if i've seen that object before and if i have i don't create a new object. After creating the Team or skipping that part i add the relevant info to the team object and continue.

My issue is the list i'm using to hold the final team info has many duplicates mean my check to see if the object exists or not doesn't work. The code is :

After splitting string,

        List<Team> teams = new List<Team>();

        for (int i = 1; i <= matches.Length - 1; i++)
        {
            string fullStr = matches[i];

            string[] score = fullStr.Split(',');

            string[] team1 = score[0].Split('!');
            string team1Name = team1[0];

            Team teams1 = new Team(team1Name);
            if (teams.Contains(teams1) != true)
            {
                teams.Add(teams1);
            }

            string team1Score = team1[1];
            int team1ScoreInt = int.Parse(team1Score);

            string[] team2 = scores[1].Split('!');
            string team2Name = team2[1];


            Team teams2 = new Team(team2Name);

            if (!teams.Contains(teams2))
            {
                teams.Add(teams2);
            }

When i print the list i get the format i want but multiple Germanys etc. And only the score etc of that 1 game rather than them all adding to 1 Germany Team object.

Any ideas how i can stop the duplicates and maintain using only the 1 Team object every time i see that team name?

Thanks

Stefan101
  • 81
  • 1
  • 6

2 Answers2

0

You can implement IEqualityComparer for Team class and check equality of objects based on its values. Something like below.

    using System.Collections.Generic;
    public class Team{
        public string Name {get;set;}
        public int score {get;set;}
        }
        //Create some dummy data
    public List<Team> lstTeam = new List<Team>{
        new Team{Name="A", score=1},
        new Team{Name="A", score=1},
        new Team{Name="B", score=1},
        new Team{Name="C", score=2},
        new Team{Name="A", score=2},
        new Team{Name="C", score=2}

        };   

     List<Team> lstDistictTeams = lstTeam.Distinct<Team>(new DistinctComparer()).ToList();
     foreach(Team t in lstDistictTeams)
     {
         Console.WriteLine("Team {0} has Score {1}",t.Name,t.score);
         }

//This class provides a way to compare two objects are equal or not
     public class DistinctComparer : IEqualityComparer<Team>
        {
            public bool Equals(Team x, Team y)
            {
                return (x.Name == y.Name && x.score == y.score); // Here you compare properties for equality
            }
            public int GetHashCode(Team obj)
            {
                return (obj.Name.GetHashCode() + obj.score.GetHashCode());
            }
        } 

Here is running example : http://csharppad.com/gist/6428fc8738629a36163d

Hakunamatata
  • 1,275
  • 13
  • 18
0

Looks like the problem is you're creating a new Team object with the name of the team for each result.

When you then compare against the list to see if it is contained, you're checking to see if there's a reference to that object in the list, and as you've just created it, there won't be, so every Team object you create will be added to the list.

You'll need to check whether the list contains a Team object with the name, rather than just check for the instance of the object.

Chris K
  • 435
  • 2
  • 10