Question

I have list of integer list ,I'm working in shift coding so i have different length code for each stream, so i use list of list for each one because its re-sizable, i tried to store all the stream in one array of bytes and i fail so how can i converted to array of bytes...

like this:

list[10 item]
list[0] of list[5 item][0,0,1,0,3]
list[1] of list[4 item][0,0,1,1]

and convert it to array of bytes...

like this: array[bytes]=[0,0,1,0,3,0,0,1,1,3,0,0,.....]; the count of item in the inner list different from one item to another....

Was it helpful?

Solution

I think SelectMany is what you're looking for:

List<List<int>> foo = new List<List<int>> { new List<int> { 1, 2, 3 }, new List<int> { 1, 2 } };
var flat = foo.SelectMany(x => x).ToList();

Flat is now: 1, 2, 3, 1, 2

OTHER TIPS

SelectMany gives you what you need:

var list = new List<List<int>>();
list.Add(new List<int>() {1, 2, 3});
list.Add(new List<int>() {4, 5, 6});
list.Add(new List<int>() {7, 8, 9});
var combined = list.SelectMany(x => x).Select(x=>(byte)x).ToArray();

At the end, you have a flattened array of bytes from a list of list of ints. Sounds like exactly what you're going for.

If you're happy with LINQ then you can "flatten" lists of lists and the like by using .SelectMany().

Like:

var array = listOfLists.SelectMany(x => x).ToArray();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top