4

I'm having some trouble selecting a byte[] from inside of a list of objects model is setup 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 down one level so I am left with a List<List<byte[]>> using LINQ so far I have:

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

but it is throwing:

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 selecting the byte array as a byte?

Some guidance would be appreciated!

4
  • 3
    Firstly, it would be a really good idea to follow .NET naming conventions, e.g. Container and Image. Commented Nov 24, 2014 at 20:09
  • @Jon- sorry these are dummy names not copy/pasted from my code Commented Nov 24, 2014 at 20:33
  • 1
    If you're using better names in your code, then please use better names in your examples, too. Anything unconventional detracts from the readability of the code - and even though this code isn't going into production, you're still asking people to read it. Commented Nov 24, 2014 at 21:03
  • Ah yes I completely didn't consider that. I'll keep that in mind in the future! Thanks for your help Commented Nov 24, 2014 at 21:24

1 Answer 1

13

You don't want SelectMany for the image property - that's going to give a sequence of bytes. For each list of containers, you want to transform that to a list of byte arrays, i.e.

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

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

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

Note the calls to ToList in each case to convert an IEnumerable<T> to a List<T>.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.