简体   繁体   中英

Convert List<Byte[]> to Byte[]

I use Gtk# and I need to convert List<Byte[]> to Byte[] . I found sample of this operation here , but it's .NET Framework's sample and when I try to do it by Gtk#, I have some compile errors:

"Type System.Collections.Generic.List<byte[]>' does not contain a definition for SelectMany' and no extension method SelectMany' of type System.Collections.Generic.List' could be found (are you missing a using directive or an assembly reference?) (CS1061)".

How can I solve this error or what other Gtk#'s ways for convert I can use?

Make sure:

  1. You are using .NET framework 3.5 or higher.
  2. You have a reference to System.Core.
  3. You have "using System.Linq" in the top of your codefile.

The extensions to IEnumerable are part of the later .net framework versions, Gtk# may not have them or they may be under another name.

A simple (not sure if there is an easier way to do it) method would be to calculate the total size that the array would need to be and initialize it to that size. Then loop through the list and place the data in each byte[] into the new byte[] by keeping track of how far through the array you are.

private static Byte[] ConvertList(List<Byte[]> list)
    {
        int totalLength = 0;

        foreach (byte[] b in list)
        {
            totalLength += b.Length;
        }

        byte[] result = new byte[totalLength];

        int currentPosition = 0;

        foreach (byte[] byteArray in list)
        {
            foreach (byte b in byteArray)
            {
                result[currentPosition] = b;
                currentPosition++;
            }
        }

        return result;
    }
private static Byte[] ConvertList(List<Byte[]> list)
    {
        List<Byte> tmpList = new List<byte>();
        foreach (Byte[] byteArray in list)
            foreach (Byte singleByte in byteArray)
                tmpList.Add(singleByte);
        return tmpList.ToArray();
    }

Pretty much a shorter version of the other code example. Tested against .Net Framework 2.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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