Combining 2 Lists

For example, I have these two lists:

List<string> firstName = new List<string>(); List<string> lastName = new List<string>(); 

How can I concatenate firstName [0] and lastName [0], firstName [1] and lastName [1], etc. and put it on a new list?

+6
source share
2 answers

It looks like you want Zip from LINQ:

 var names = firstName.Zip(lastName, (first, last) => first + " " + last) .ToList(); 

(Actually, this is not a union of two lists that combine them by elements, but in reality it is not the same thing.)

EDIT: If you are using .NET 3.5, you can enable the Zip method yourself - Eric Lippert's blog post sample code.

+7
source

You can use the Linq Concat and ToList methods

var lists = firstname.Concat (lastname) .ToList ();

0
source

Source: https://habr.com/ru/post/953540/


All Articles