icarus
icarus

Reputation: 166

merging 2 string lists

suppose I have 2 lists

List<string> list1 = new List<string>;
list1.Add("one");
list1.Add("three");
list1.Add("five");

List<string> list2 = new List<string>;
list2.Add("two");
list2.Add("four");
list2.Add("six");

how could I go about merging them (technically not concatenating) so that I can get a new list with the values:

[one two] [three four] [five six]

Note that the '[' and ']' delimits each string in the final list. So the first value in the list is 'one two', the second value is 'three four' and the third value is 'five six'.

I hope I explained it in a clear manner.

Upvotes: 0

Views: 60

Answers (1)

D Stanley
D Stanley

Reputation: 152624

The Linq function you want is Zip:

var list3 = list1.Zip(list2, (s1, s2) => s1 + " " + s2);

Output:

IEnumerable<String> (3 items)
---------------
one two 
three four 
five six 

Upvotes: 6

Related Questions