Avinash Karat
Avinash Karat

Reputation: 25

Pair values from two lists

I have two lists with me as per the following example.

List<string> words = new List<string>() {"V","H","M" };

List<int> numbers = new List<int>() {10,20,30 };

I need to pair the values of these two lists so that my output needs to be exactly like the following text.

Desired output : V10 H20 M30

Upvotes: 0

Views: 217

Answers (3)

Innat3
Innat3

Reputation: 3576

I'm a bit late to the party but here's a very simple way of doing it without Zip: (x = item, y = index)

var mergedList = words.Select((x, y) => $"{x}{numbers.ElementAt(y)}");

Upvotes: 0

Nico
Nico

Reputation: 3542

You could use Zip method for that.

You can try the following:

String.Join(" ", words.Zip(numbers, (first, second) => first + second))

Upvotes: 6

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186668

Try using Zip:

var result = words
  .Zip(numbers, (w, n) => $"{w}{n}");

Console.Write(string.Join(" ", result));

Upvotes: 6

Related Questions