[英]Merge multiple byte arrays c#
I want to merge multiple byte arrays but failing.The final array shows the last added byte array only, not all byte arrays. 我想合并多个字节数组但失败了。最终数组仅显示最后添加的字节数组,而不是所有字节数组。 Following is my try. 以下是我的尝试。
List<byte[]> d = new List<byte[]>();
foreach (var item in IDs)
{
obj = RequisitionsObj.GenerateLabOrderReq();
if (obj.Data != null)
{
d.Add(obj.Data);
}
}
byte[] final = Combine(d.SelectMany(a => a).ToArray());
private byte[] Combine(params byte[][] arrays)
{
byte[] rv = new byte[arrays.Sum(a => a.Length)];
int offset = 0;
foreach (byte[] array in arrays)
{
System.Buffer.BlockCopy(array, 0, rv, offset, array.Length);
offset += array.Length;
}
return rv;
}
You don't need the Combine
method. 您不需要Combine
方法。 Just use SelectMany
: 只需使用SelectMany
:
List<byte[]> d = new List<byte[]>();
foreach (var item in IDs)
{
obj = RequisitionsObj.GenerateLabOrderReq();
if (obj.Data != null)
{
d.Add(obj.Data);
}
}
byte[] final = d.SelectMany(a => a).ToArray();
EDIT 编辑
A working sample: 工作样本:
List<byte[]> d = new List<byte[]>();
byte[] b1 = new byte[] { 1, 2, 3, 4 };
byte[] b2 = new byte[] { 5, 6, 7, 8 };
d.Add(b1);
d.Add(b2);
byte[] b3 = d.SelectMany(a => a).ToArray(); // Content is 1,2,3,4,5,6,7,8
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.