从 Linq 中的 Select 查询中查找索引

hel*_*llo 4 c# linq

我试图找到indexa 中两个属性之间的差异在List<>哪里max?目前,我发现max使用LINQ以下查询:

var result = link.Select(x => Math.Abs(x.Prop1 - x.Prop2)).Max();
Run Code Online (Sandbox Code Playgroud)

我怎样才能得到索引?

myb*_*ame 5

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;
Run Code Online (Sandbox Code Playgroud)

这是工作。