model isn't working as expected C#

c# dapper mapping model

Question

My model is getting data I need, but I'm not sure how to organize it in the way I need. I have it where the model pulls a team, and a player. The issue is when mapping my data to the model it gives me duplicate teams, and doesn't allow me to call players individually. essentially I have a new object for each player, but I want a new object for each team instead, and without duplicates. I also want each team to have a player property where I can pull an individual player.

Here is what I have so far.

Current method

public HashSet<Player> getPlayers()

    {
        var myHashset = new HashSet<Player>();

        const string storedProc = "aProc";

        dynamic players;

        using (var conn = DBconnection.myConnection())
        {
        players = conn.Query(storedProc, commandType: CommandType.StoredProcedure);
        }

        foreach (var record in players)
        {
            var myPlayer = new Player
            {
                team = record.team,
                player = record.player
            };

            if (myPlayer != null)
            {
                myHashset.Add(myPlayer);
            }
        }
        return myHashset;

Model

public class Player
{
    public string team { get; set; }
    public string player { get; set; }

}

So in conclusion I need it to return an object that checks the data to see if the team is a duplicate. If it is then add the player to the team that currently exists. If it is a new team then create the team, and add the player.

What stored Proc returns when executed in visual studio

It returns a table that is laid out like this.

RowID --- player --- Team
1 --- roy --- bengals
2 --- nick --- bengals
3 --- jeff --- browns
4 --- carl --- steelers

Accepted Answer

I suggest you review your model structure.

public class Team
{
    public string Name { get; set; }
    public List<string> Players { get; set; }
}

then in your loop

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

foreach (var record in players)
{
    Team team = teams.SingleOrDefault(q=>q.Name == record.team);

    if(team == null)
    {
         team = new Team();
         team.Name = record.team;
         team.Players = new List<string>();
         team.Players.Add(record.player);
         teams.Add(team);
    }
    else
    {
        team.Players.Add(record.player);
    }       
}

then return your list of teams (don't forget to change your method return type)

return teams;

Popular Answer

From the comments made on your question, I think the following may be helpful. However, for the record, none of this is best practice, and I can't professionally recommend other people do this. But it solves the issue you have with the constraints you're under.

If you just want to get the list of teams with no duplicates, you could do the following:

List<string> teams = myHashSet.Select(x => x.team).Distinct().ToList();

This will return a distinct list of teams. Essentially, if you want to get individual teams, and just pull through one player, you're going to have to manually go through your HashSet after you have built it up.

EDIT: Some information in the comments below the question itself have been removed (or I can't see them!) but alphamalle did mention that they do not have access to modify the stored procedure, hence my approach above.



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why