列表中的选择项与其他列表中保存的索引相关吗?

时间:2017-10-06 00:48:12

标签: c# list

我有:
列表x列表;
列表A的列表,其包含列表d中的一些元素的索引,如A [i] = [1,3]
包含d [i] = [500,200,1000,40,60,..]
的值的列表d列表 我想从d中保存了索引的元素中选择并将它们添加到x
那么x [0]将= [200,40]

我使用此代码:

select . . .,
       max(case when seqnum <= cnt * 0.25 then x.MBperSec end) over (partition by x.block, x.operation) as percentile_25,
       max(case when seqnum <= cnt * 0.75 then x.MBperSec end) over (partition by x.block, x.operation) as percentile_75
from (select x.*,
             row_number() over (partition x.block, x.operation
                                order by x.MBperSec
                               ) as seqnum,
             count(*) over (partition x.block, x.operation) as cnt
      from Table_CPU x
      where x.Operation = 'reading' and
            replace(x.block, 'KB', '') in ('64')
     ) x

你能帮我用一种有效的方法吗?

2 个答案:

答案 0 :(得分:0)

List<int> sublist = new List<int>();
foreach(int i in A)
{
    sublist.Add(d[i]);
}
x.Add(sublist);

答案 1 :(得分:0)

这真的取决于你的意思&#34;效率&#34;。

如果你想要最快的执行时间,那么嵌套的for循环可能是你最好的选择。您可以通过指定sublist的初始大小以匹配A[i1]中的元素数量,并删除.Count()扩展方法以支持{{1}来节省一些时间}}或.Count属性,具体取决于您使用的确切类型。

如果您只想要简单的代码,那么LINQ可以提供帮助:

.Length

Demo

或者:

List<List<int>> x = A
    .Select((value, index) => value.Select(i => d[index][i]).ToList())
    .ToList();

Demo