C#LINQ返回计数器数组索引max到min

tar*_*zan 4 c# linq

C#和LINQ的新手.我有一个基本上是计数序列的数组.

{1,3,5,2,7,2}

我正在尝试编写一个查询,该查询以降序返回具有最高值的索引列表:

4,2,1,3,5,0

我可以通过下面的查询获得最大索引,但我似乎无法弄清楚如何使用单个查询按顺序获取下一个索引.

int index = array.ToList().IndexOf(array.Max());
Run Code Online (Sandbox Code Playgroud)

Eni*_*ity 10

这有效:

var list = new [] {1,3,5,2,7,2};

var indices =
    list
        .Select((n, i) => new { n, i })
        .OrderByDescending(x => x.n)
        .Select(x => x.i)
        .ToArray();
Run Code Online (Sandbox Code Playgroud)


Rah*_*ngh 5

您可以使用Select:-

var result = numbers.Select((v, i) => new { Value = v, Index = i })
                                .OrderByDescending(x => x.Value)
                                .Select(x => x.Index).ToArray();
Run Code Online (Sandbox Code Playgroud)

工作小提琴