C# 使用linq函数在字典中查找字典的值

C# 使用linq函数在字典中查找字典的值,c#,linq,C#,Linq,我试图找出如何从字典中检索单个值和值列表,该字典是我创建的类的变量,位于另一个字典中。我将只写下代码和我迄今为止尝试过的内容,以便更容易理解 public class Team { public UInt32 TeamID; public Dictionary<UInt32, Player> Players; (...) } public class Match { public UInt32 MatchID; public Dictiona

我试图找出如何从字典中检索单个值和值列表,该字典是我创建的类的变量,位于另一个字典中。我将只写下代码和我迄今为止尝试过的内容,以便更容易理解

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>();

我相信第二件事可以正常工作,但我似乎不知道如何获得前面提到的分数之和。

您试图用一个LINQ查询做太多的事情。这是可以做到的,但将其分解为GetScore和GetWinner将更具可读性。我想说,一支球队能够计算自己的得分,一场比赛能够计算自己的胜利者是有道理的。
// 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];
    (...)
}
// Get the Team instance that contains a certain player.

public Team GetPlayerTeam(UInt32 PlayerID)
{
    return Participants.Values.Where(x => x.Players.ContainsKey(PlayerID)).FirstOrDefault();
}