-6

我有这样的结构

List<int[]> nodeIds = new List<int[]>();

我可以从中获取每个元素int[]吗?我该怎么做?

4

5 回答 5

3

例如:

foreach(int[] items in nodeIds)
  foreach(int item in items)
    // do something with item
于 2013-02-19T08:15:22.653 回答
1

您可以使用 Linq Enumerable.SelectMany方法将序列展平为一个序列:

IEnumerable<int> allIds = nodeIds.SelectMany(x => x);

更新:如果您只需要结果序列中的唯一 ID,那么在讨人喜欢的序列之后也应用Enumerable.Distinct 。

于 2013-02-19T08:14:36.480 回答
0

你有一个Listofint[]所以你需要做的是获取List. 您将不得不为此使用嵌套循环 - 一个在列表上,第二个在当前考虑的列表项的数组上。

于 2013-02-19T08:14:26.843 回答
0

就像是:

foreach(var nodeId in nodeIds)
{
    foreach(var id in nodeId)
    {
        //do stuff with id
    }
}

或者

nodeIds[0][0]
于 2013-02-19T08:15:08.420 回答
0

如果要从列表中的所有数组中获取所有整数,可以使用以下SelectMany方法IEnumerable

IEnumerable<int> allIDs = nodeIds.SelectMany(a => a);

foreach您可以使用或对结果执行聚合等来枚举结果。

于 2013-02-19T08:16:07.723 回答