Using LINQ to Select an Array of Bytes

I am having trouble selecting byte [] from within a list of objects that are set as:

public class container{
    public byte[] image{ get;set; }
    //some other irrelevant properties    
}

      

in my controller i have:

public List<List<container>> containers; //gets filled out in the code

      

I'm trying to pull image

one level, so I'm left with List<List<byte[]>>

, using LINQ so far I:

var imageList = containers.Select(x => x.SelectMany(y => y.image));

      

but it throws:

cannot convert from 
'System.Collections.Generic.IEnumerable<System.Collections.Generic.IEnumerable<byte>>' to 
'System.Collections.Generic.List<System.Collections.Generic.List<byte[]>>'  

      

Apparently it is picking the byte array as byte?

Some advice would be appreciated!

+3


source to share


1 answer


You don't want SelectMany

for a property image

that will give you a sequence of bytes. For each list of containers, you want to convert this to a list of byte arrays i.e.

innerList => innerList.Select(c => c.image).ToList()

      

... and then you want to apply this projection to your outer list:



var imageList = containers.Select(innerList => innerList.Select(c => c.image)
                                                        .ToList())
                          .ToList();

      

Pay attention to the calls ToList

in each case to convert IEnumerable<T>

to List<T>

.

+11


source







All Articles