简体   繁体   English

将锯齿状数组转换为IEnumerable <KeyValuePair<int,int> &gt;?

[英]Convert Jagged Array to IEnumerable<KeyValuePair<int,int>>?

I have this Jagged Array : 我有这个锯齿数组:

( n is given at compile time , so please assume that all values are already there) n是在编译时给出的,因此请假定所有值都已经存在)

int[][] jagged = new int[n][];

jagged[0] = new int[3];
jagged[0][0] = 2; // Please ignore the dummy values....
jagged[0][1] = 55;
jagged[0][2] = 4;

jagged[1] = new int[3];
jagged[1][0] = 6;
jagged[1][1] = 3;
jagged[1][2] = 7;

...
...
jagged[n] = new int[3];
jagged[n][0] = 9;
jagged[n][1] = 5;
jagged[n][2] = 1;

I want to create a IEnumerable<KeyValuePair<int,int>> from this structure where : 我想从此结构创建IEnumerable<KeyValuePair<int,int>> ,其中:

key is : dummy value and the value is the n ( dummy value is unique) 关键是: dummy value ,该数值n (哑数值是唯一的)

(aside info - I need to map pictures to users) (除了信息-我需要将图片映射到用户)

So for group #1 I want: 因此,对于组#1我想:

  {2-> 0}
  {55-> 0}
  {4-> 0}

for group #2 对于#2

  {6 -> 1}
  {3-> 1}
  {7-> 1}
  ...
  ...
  etc

But as a whole list : 但总体而言:

So final result should be 1 IEnumerable of KeyValuePair<int,int> : 因此最终结果应为KeyValuePair<int,int> 1 IEnumerable

  {2-> 0}
  {55-> 0}
  {4-> 0}
  {6 -> 1}
  {3-> 1}
  {7-> 1}
  ...
  {9 -> n}
  {5-> n}
  {1-> n}

Question : 题 :

  • Is there any Linq'y way of doing it ?(instead of loops over loops ) ? 有什么Linq'y的方法吗?(而不是遍历循环)?
foreach (var t in jagged.SelectMany((row, rowIndex) => row.Select(value => new KeyValuePair<int, int>(value, rowIndex))))
{
    Console.WriteLine("{0} - {1}", t.Key, t.Value);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM