0

i have 2 lists:

List 1 = {"1", "2", "", "", "4", ""}

List 2 = {"3", "4", "5", "", "6", ""}

I want a new List:

List 3 = {"1, 3", "2, 4", "5", "", "4, 6", ""};

How can i get this result?

Gustavo B.
  • 53
  • 5
  • 3
    See [What is the use of Enumerable.Zip extension method in Linq?](https://stackoverflow.com/q/5122737/402022). – Theraot Apr 11 '21 at 03:33

1 Answers1

3

You can use Enumerable.Zip with string.Join for this:

var result = list1
  .Zip(list2, (a, b) => string.Join(", ", new[] { a, b }.Where(x => x != "")))
  .ToList();

Try online

torvin
  • 6,515
  • 1
  • 37
  • 52
  • I adjusted to `novaLinha.Zip(listaEstadosConcatenar, (a, b) => string.Join(", ", new[] { a, b })).Where(x => x != "").ToList()` but it produces results like "," or "2 ," or ", 3". – Gustavo B. Apr 11 '21 at 03:50
  • 2
    @GustavoB. You placed the parentheses differently – Klaus Gütter Apr 11 '21 at 04:03