我试图找到一种方法,可以在一行代码中将一种类型的2d数组转换为另一种类型。
这是一种个人学习的体验,而不是需要一行就做!!
我到目前为止都将其转换为IEnumerable<Tuple<ushort,ushort>>
。不知道从这里去哪里。
int[,] X = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 } };
var Result = (from e in X.OfType<int>() select e)
.Select(S => (ushort)S)
.Select((value, index) => new { Index = index, Value = value })
.GroupBy(x => x.Index / 2)
.Select(g => new ushort[,] { { g.ElementAt(0).Value,
g.ElementAt(1).Value } });
需要以某种方式将元组的集合转换为ushort [,]
编辑:
只是澄清问题。
如何使用linq中的单行代码将int 2d数组转换为ushort 2d数组?
编辑:
我已经更新了我的代码。
现在,我得到了一个ushort [,]的IEnumerable集合。
我现在需要找到一种方法将所有这些合并为一个ushort [,]
为了保持二维结果,我能想到的最好的办法是:
var input = new [,] { { 1, 2 }, { 3, 4 }, { 5, 6 } };
var output = new ushort[input.GetUpperBound(0) + 1, input.GetUpperBound(1) + 1];
Buffer.BlockCopy(input.Cast<int>().Select(x => (ushort)x).ToArray(), 0, output, 0, input.GetLength(0) * input.GetLength(1) * sizeof(ushort));
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句