Bugaboo
Bugaboo

Reputation: 971

Getting the max value out of a tuple in a dictionary in C#

I have a dictionary declared as following:

Dictionary<Tuple<long, long>, Tuple<double, long>> dict= new Dictionary<Tuple<long, long>, Tuple<double, long>>(); 

How do I get the max value of Item2 of all dictionary values?

Upvotes: 0

Views: 3245

Answers (2)

Kvam
Kvam

Reputation: 2218

You can use the Max method. Not entirely clear which one of these you're asking for:

var maxKey = dict.Max(x => x.Key.Item2);
var maxValue = dict.Max(x => x.Value.Item2);

var maxEither = Math.Max(dict.Max(x => x.Key.Item2), dict.Max(x => x.Value.Item2));

Upvotes: 3

Yacoub Massad
Yacoub Massad

Reputation: 27861

You can simply use the Max LINQ method like this:

long max = dict.Values.Max(x => x.Item2);

Upvotes: 3

Related Questions