I have a list of immutable objects (in my specific case a list of Tuple<double, double>
) and I'd like to change the one with the highest Item2 value.
Ideally there would be an IndexOfMaxBy function I could use, so I could do:
var indexOfPointWithHighestItem2 = myList.IndexOfMaxBy(x => x.Item2);
var original = myList[indexOfPointWithHighestItem2];
myList[indexOfPointWithHighestItem2] =
new Tuple<double, double>(original.Item1, original.Item2 - 1);
I have seen How can I get LINQ to return the object which has the max value for a given property?, and using Jon Skeet's MaxBy function combined with Select I could do:
var indexOfPointWithHighestItem2 =
myList.Select((x, i) => new { Index = i, Value = x })
.MaxBy(x => x.Item2).Index;
But this creates a new object for every object in my list, and there must be a neater way. Does anyone have any good suggestions?