I have one list of list { {1, 3, 5}, { 2, 4, 6}} another list of list {{7}, {8}}
Is there a quick to generate list { {1, 3, 5, 7}, {2, 4, 6, 8}}
I have one list of list { {1, 3, 5}, { 2, 4, 6}} another list of list {{7}, {8}}
Is there a quick to generate list { {1, 3, 5, 7}, {2, 4, 6, 8}}
I have one list of list { {1, 3, 5}, { 2, 4, 6}} another list of list {{7}, {8}} Is there a quick to generate list { {1, 3, 5, 7}, {2, 4, 6, 8}}
Yes: use the Zip
sequence operator.
IEnumerable<IEnumerable<int>> lists1 = whatever;
IEnumerable<IEnumerable<int>> lists2 = whatever;
List<List<int>> zipped = lists1
.Zip(lists2, (list1, list2) => list1.Concat(list2).ToList())
.ToList();
Follow along.
IEnumerable<int>
. We want a List<int>
, so ToList
it.IEnumerable<List<int>>
. List<List<int>>
, so we ToList
the whole thing.This is the technique you should use when writing LINQ queries; just break everything down into a workflow of simpler steps, and then combine them together.