c# - Get maximum value from Dictionary -
i have dictionary of type
dictionary<int, gvalue>
where gvalue object contains 2 double values p1 , p2
how can max p1 , p2 dictionary?
i have tried far
c.calculategraphmetrics(nodexlcontrol1.graph).max(s => s.value.p1);
it gives me no error, result shows on debug
expression cannot contain lambda expressions
re. getting key corresponding maximum value: see lot of complicated implementations of maxby
when can use 1 of overloads of aggregate
similar effect:
var keyformaxp1 = dict.keys.aggregate((i, j) => dict[i].p1 >= dict[j].p1 ? : j); var keyformaxp2 = dict.keys.aggregate((i, j) => dict[i].p2 >= dict[j].p2 ? : j);
edit: if want multiple maximum keys, you'll need like
var allmaxkeysforp1 = dict.keys.aggregate( new { value = double.negativeinfinity, keys = new list<int>() }, (a, k) => { if (a.value > dict[k].p1) return a; if (a.value.equals(dict[k].p1)) { a.keys.add(k); return a; } return new { value = dict[k].p1, keys = new list<int> { k } }; }, => a.keys);
at point might want consider implementing allmaxby
method.
Comments
Post a Comment