I'm building an application for a game. On my form I have a datagridview to which I would like to bind the choices that were made by the different players.
I have a specific PlayerClass which I have put in a list called Players.
public class PlayerClass
{
public int Id {get; set;}
public string Name {get; set;}
public List<bool> choices {get; set;}
}
Public List<PlayerClass> Players;
In this list I will add the choices made by the players for the different questions.
To display the choices made, I want to combine the results into a datagridview where I have one column for every player andwhere every row represents the answer given by that player for that specific question.
So I was thinking of creating a list with a new Class which holds all the answers that were given by the different players for a question.
public class ChoicesViewClass
{
public bool ChoicePlayer1 { get; set; }
public bool ChoicePlayer2 { get; set; }
public bool ChoicePlayer3 { get; set; }
public bool ChoicePlayer4 { get; set; }
}
But how can I use Linq to populate the Choices in the secondary list from the first list?
I was trying something like this:
grdChoices.DataSource = Players
.Select(s => new ChoicesViewClass{ ChoicePlayer1 = s, ChoicePlayer2 = s, ChoicePlayer3 = s, ChoicePlayer4 = s })
.ToList();
But the s
is where I am stuck.
How can I solve this?
Thank you.
Well you are trying to access the player choices by index? will the player choices list have 4 length by default? This will solve your problem:
grdChoices.DataSource = Players
.Select(s => new ChoicesViewClass{ ChoicePlayer1 = s.choices[0], ChoicePlayer2 = s.choices[1], ChoicePlayer3 = s.choices[2], ChoicePlayer4 = s.choices[3] })
.ToList();
But i'm a bit confused, why complicate it to much? why can't the player have a property of ChoicesViewClass? like so :
public class PlayerClass
{
public int Id {get; set;}
public string Name {get; set;}
public ChoicesViewClass choices {get; set;}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With