-1

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}}

Pat
  • 11
  • 2
  • 1
    Please post your attempt. – itsme86 Aug 31 '18 at 23:00
  • array1.Zip(array2, (a,b)=>a.AddRange(b)) – Aryan Firouzian Aug 31 '18 at 23:13
  • 4
    @AryanFirouzian: No, **never do that**. You never want the selection of a zip operator to have a side effect on one of its operands! If that's the desired effect then use a loop always, *never* `Zip`. Remember, *queries read data*. Use a loop if you wish to *write* data. – Eric Lippert Aug 31 '18 at 23:15
  • @EricLippert can you be more accurate. This is the way we always use Zip. Is it different from your answer? – Aryan Firouzian Aug 31 '18 at 23:27
  • @AryanFirouzian: If that is the way that you always use zip then *you are always using it wrong*. Stop doing that! It is extremely dangerous to use a selector as a loop. It is very easy to write a program that looks correct and works, and is extremely brittle when you make even a small change. **Only use sequence operators to read from a sequence, never to write to it**. – Eric Lippert Aug 31 '18 at 23:41
  • @AryanFirouzian: Yes, it is **very different** from my answer. My answer *produces a brand new sequence of fresh lists*. Your comment *mutates existing lists*. Producing a new value is not *writing to a member*. Mutating an existing list is writing to a member. Again: **a query operation must never have an observable side effect on its operands**. If you're doing that, then you're doing something wrong. – Eric Lippert Aug 31 '18 at 23:43
  • I got it now, you were refering to first two lines. Thanks.You are completely right. – Aryan Firouzian Aug 31 '18 at 23:46
  • 1
    @AryanFirouzian: I was referring to your use of `AddRange`, a side-effecting operation, inside a `Zip`, which is required to be not-side-effecting. Use `Concat`, which is non-side-effecting. Never use `AddRange` in a sequence operation. – Eric Lippert Aug 31 '18 at 23:47
  • Oh, I didnt know that much about that.Thanks for advice. Already found [material](https://stackoverflow.com/questions/100196/net-listt-concat-vs-addrange) to read. – Aryan Firouzian Aug 31 '18 at 23:50

1 Answers1

4

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.

  • We have two sequences which contain sequences.
  • Zip takes one from the first and one from the second, in pairs, and combines them
  • The combination is to concatenate list2 to the end of list1.
  • That gives you an IEnumerable<int>. We want a List<int>, so ToList it.
  • The result of Zip is an IEnumerable<List<int>>.
  • We want a 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.

Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067