I have a player class
public class Player : Person
{
public List<int> SetPoints;
}
And a game class
private List<Player> Players;
public Game(List<Player> players)
{
Players = players;
}
public void Simulate()
{
var winnerPoints = 6;
var looserPoints = Tournament.Random.Next(0, 5);
var winnerId = Players[Tournament.Random.Next(0, 1)].Id;
Players.Where(p => p.Id == winnerId).Select(p => p.SetPoints).Add(winnerPoints);
}
The code demonstrates what im trying to accomplish but i cant access the SetPoints list for updating, in the selected player object. Can anyone point out what modification i need to make in order for this to work?
Thanks in advance
UPDATED
My goal was to add winnerPoints to SetPoints of one random Player object in Players, and loserPoints to the other (Players is always a list of two objects, provided by the constructor) thanks to maccettura i accomplished this by using FirstOrDefault()
public void Simulate()
{
var winnerPoints = 6;
var loserPoints = Tournament.Random.Next(0, 5);
var winnerId = Players[Tournament.Random.Next(0, 1)].Id;
Players.FirstOrDefault(p => p.Id == winnerId).SetPoints.Add(winnerPoints);
Players.FirstOrDefault(p => p.Id != winnerId).SetPoints.Add(loserPoints);
}
FirstOrDefault()instead ofWhere().Select(). Also,looserPointsshould probably beloserPoints(its a typo)Select()will return anIEnumerable<List<int>>not aList<int>.FirstOrDefault()is the same logic you have now (but working). Also, there is nothing "random" about the code you have posted. If there is some other requirement it needs to be included in the question. Please see how to add a minimal reproducible example