gpt4 book ai didi

c# - 将交错数组转换为 IEnumerable>?

转载 作者:太空宇宙 更新时间:2023-11-03 20:05:45 25 4
gpt4 key购买 nike

我有这个锯齿状数组:

(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;

我想创建一个 IEnumerable<KeyValuePair<int,int>>从这个结构中:

key 是:dummy valuen (虚拟值是唯一的)

(除了信息 - 我需要将图片映射给用户)

所以对于组#1我要:

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

#2

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

但作为一个整体列表:

所以最终结果应该是 1 IEnumerableKeyValuePair<int,int> :

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

问题:

  • 是否有任何 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);
}

关于c# - 将交错数组转换为 IEnumerable<KeyValuePair<int,int>>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23585670/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com