0

What I want to do is basically

var max = things.Select(t => ExpensiveFunc(t)).Max();
var ThingWithMaxResult = things.Where(t => ExpensiveFunc(t) == max).First();

But I don't want to have to run the ExpensiveFunc twice on each element.

I am learning LINQ so I would like to know the LINQ way of doing this. Otherwise I would normally create an array of things and results, then just pick the array with the highest result.

CuriousDeveloper
  • 750
  • 1
  • 6
  • 20

1 Answers1

0

You can order by the function call (descending) and take the first one:

var max = things.OrderByDescending(ExpensiveFunc).First();
Rufus L
  • 32,853
  • 5
  • 25
  • 38