避免使用linq创建新对象

use*_*051 0 c# linq

我对linq语法不是很熟悉,我不确定这个查询是否与我想要实现的完美契合,但它的工作完美.我怎样才能让它更好,是否可以避免使用新的元组或新对象?

byte[] ShuffledBytes= new byte[20];
byte[] Indecies = new byte[20]; //holds the right index for each byte in Bytes

    var orderdBytes = 
                    Indecies.
                    Zip(ShuffledBytes, (i, b) => new Tuple<byte,byte>(b,i)).
                    OrderBy(o => o.Item2).
                    Select(o => o.Item1).
                    ToArray();
Run Code Online (Sandbox Code Playgroud)

tin*_*afl 6

我想你想要的Array.Sort不是LINQ.其中一个重载使用一个数组作为键,另一个作为要排序的项:

Array.Sort(Indecies, ShuffledBytes);
Run Code Online (Sandbox Code Playgroud)

不需要额外的对象ShuffledBytes根据Indecies中的值进行排序.

另一个选择是使用排序字典或keyvaluepair列表,而不是2个数组.