问题描述
亲爱的,
我有以下二维数组:
Dear all,
I have the following Two Dimensional Array:
object[,] cellValues
[1,1 ] =x
[1,2] =y
[1,3] =z
[2,1] =a
[2,2] =b
[2,3] =c
[3,1] =我
[3,2] =j
[3 ,3] =k
[4,1] =p
[4,2] =q
[4,3] =r
[5,1] =m
[ 5,2] =n
[5,3] =o
现在我想改造上面的两个字典或列表中的维数:
1 [x,y,z]
2 [a,b,c]
3 [i,j,k]
4 [p,q,r]
5 [m,n,o]
如何使用LINQ和/ foreach这样做需要很长时间才能完成此操作?
请注意,Two Dimensional数组包含513360个值。
我尝试了什么:
[1,1] = "x"
[1,2] = "y"
[1,3] = "z"
[2,1] = "a"
[2,2] = "b"
[2,3] = "c"
[3,1] = "i"
[3,2] = "j"
[3,3] = "k"
[4,1] = "p"
[4,2] = "q"
[4,3] = "r"
[5,1] = "m"
[5,2] = "n"
[5,3] = "o"
Now I am trying to transform the above Two Dimensional array into Dictionary or List:
1 [x,y,z]
2 [a,b,c]
3 [i,j,k]
4 [p,q,r]
5 [m,n,o]
How to do this using LINQ as for/foreach is taking long time to complete this?
Please note that, the Two Dimensional array containf 513360 values.
What I have tried:
var xLimit = Enumerable.Range(0, cellValues.GetUpperBound(0) + 1);
var yLimit = Enumerable.Range(0, cellValues.GetUpperBound(1) + 1);
var result = xLimit.SelectMany(x => yLimit.Select(y => cellValues[x, y]));
推荐答案
var xLimit = Enumerable.Range(0, cellValues.GetUpperBound(0) + 1);
var yLimit = Enumerable.Range(0, cellValues.GetUpperBound(1) + 1);
var result = xLimit.AsParallel().SelectMany(x => yLimit.AsParallel().Select(y => cellValues[x, y]));
了解更多详情; []
public class Model
{
public List<string> Values { get; set; }
}
string[,] parts = new string[,] {{"a","b","c"},{"d","e","f"}};
List<Model> list = new List<Model>();
//find out how big our array is
int dim1 = parts.GetLength(0);
int dim2 = parts.GetLength(1);
for (int i = 0; i < dim1; i++)
{
List<string> values = new List<string>();
for (int j = 0; j < dim2; j++)
{
values.Add(parts[i,j]);
}
list.Add(new Model(){ Values = values });
}
这篇关于二维数组按或列出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!