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
  • 849
  • 2
  • 8
  • 27

1 Answers1

0

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

var max = things.OrderByDescending(ExpensiveFunc).First();
Rufus L
  • 36,127
  • 5
  • 30
  • 43