我有一个字典,我想找到具有最高价值的密钥.
例如,如果我有
Key | Value
a | 1
b | 2
c | 3
Run Code Online (Sandbox Code Playgroud)
我希望c被退回.
我现在有这个,但它只搜索最高值,我不知道如何返回密钥.
var max = occurrences.Max(x => x.Value);
Run Code Online (Sandbox Code Playgroud)
var maxKey = occurrences.OrderByDescending(x => x.Value).First().Key;
Run Code Online (Sandbox Code Playgroud)
你需要包括MoreLinq和使用MaxBy
var result = occurrences.MaxBy(kvp => kvp.Value).Key;
Run Code Online (Sandbox Code Playgroud)