First of all, a
test
dictionary can
NOT contains such of values:
{"val1",10},
{"val2",20},
{"val1",50},
{"val3",100}
{"val1",150}
because a
val1
already exists!
You have to chenge your dictionary object to this definition:
Dictionary <int, string> test = new Dictionary<int, string>()
{
{10, "val1"},
{20, "val2"},
{50, "val1"},
{100, "val3"},
{150, "val1"}
};
Above code will create dictionary object as follow:
Key Value
10 val1
20 val2
50 val1
100 val3
150 val1
If you would like to return max for value equal to "
val1
", you can use Linq:
var maxKeyOnValue = test.Where(c=>c.Value.Equals("val1")).Select(x=>x.Key).Max();
Another idea is to use
SortedDictionary[
^].