I'm trying to figure out how to both retrieve a single value and a list of values from a dictionary that is a variable of a class I created and is inside another dictionary. I'll just write down the code and what I have tried so far for an easier understanding.
public class Team
{
public UInt32 TeamID;
public Dictionary<UInt32, Player> Players;
(...)
}
public class Match
{
public UInt32 MatchID;
public Dictionary<UInt32, Team> TeamsInMatch = new Dictionary<UInt32, Team>();
(...)
}
public Dictionary<UInt32, Team> Participants = new Dictionary<UInt32, Team>();
public Dictionary<UInt32, Match> AllMatches = new Dictionary<UInt32, Match>();
public Dictionary<UInt32, UInt32> PlayerScores = new Dictionary<UInt32, UInt32>();
Alright so, the Players dictionary inside of the team class contains the PlayerID as the key and the player variable as the value. The PlayerScores dictionary contains the individual score for each player (so the key is the PlayerID and the value his/her score). The AllMatches dictionary contain all the matches that are currently on-going (the key is the MatchID and the value the match itself).
I'm trying to use LINQ to achieve two different things:
// Get a sum of scores of all the players inside the same team and select a winner between the 2 teams playing:
foreach (KeyValuePair<uint, Match> M in AllMatches.ToList())
{
var pWinner = M.Value.TeamsInMatch[
PlayerScores.Where(x => M.Value.TeamsInMatch.ContainsKey(x.Key))
.Aggregate((l, r) => l.Value == r.Value ? (new Random().Next(2) > 0 ? l : r) : l.Value < r.Value ? l : r)
.Key];
(...)
}
And also
// Get the Team instance that contains a certain player.
public Team GetPlayerTeam(UInt32 PlayerID)
{
return Participants.Values.Where(x => x.Players.ContainsKey(PlayerID)).FirstOrDefault();
}
I believe the second thing is working properly but I can't seem to figure out how to get the sum of the scores as mentioned..