hello hello - 24 days ago 6
C# Question

Finding index from Select query in Linq

I'm trying to find the

index
at which the difference between two properties in a
List<>
is
max
? Currently, I'm finding the
max
using this
LINQ
query below:

var result = link.Select(x => Math.Abs(x.Prop1 - x.Prop2)).Max();


How can I get the index ?

Answer
var result = 
     link.Select((x, i) => new { value = Math.Abs(x.Prop1 - x.Prop2), index = i })
          .OrderByDescending(x=>x.value)
          .FirstOrDefault();

var indexValue = result?.index;
var maxValue = result?.value;

Here this is working.