相关疑难解决方法(0)

如何使用LINQ获取数组中最高值的索引?

我有一个双打数组,我想要最高值的索引.这些是我到目前为止提出的解决方案,但我认为必须有一个更优雅的解决方案.想法?

double[] score = new double[] { 12.2, 13.3, 5, 17.2, 2.2, 4.5 };
int topScoreIndex = score.Select((item, indx) => new {Item = item, Index = indx}).OrderByDescending(x => x.Item).Select(x => x.Index).First();

topScoreIndex = score.Select((item, indx) => new {Item = item, Index = indx}).OrderBy(x => x.Item).Select(x => x.Index).Last();

double maxVal = score.Max();
topScoreIndex = score.Select((item, indx) => new {Item = item, Index = indx}).Where(x => x.Item == maxVal).Select(x => x.Index).Single();
Run Code Online (Sandbox Code Playgroud)

c# linq

22
推荐指数
3
解决办法
3万
查看次数

获取最大元素的索引

给出这样一个清单:

        List<int> intList = new List<int>();
        intList.Add(5);
        intList.Add(10);
        intList.Add(15);
        intList.Add(46);
Run Code Online (Sandbox Code Playgroud)

你如何获得列表中最大元素的索引?在这种情况下,它在索引3处.

编辑:遗憾的是标准LINQ没有提供这些功能.

c# linq

21
推荐指数
4
解决办法
2万
查看次数

标签 统计

c# ×2

linq ×2