'Combining elements of the same index from two lists in C# [duplicate]
I have two lists:
list1=[1,2,3]
list2=["Ham","Cheese","Bacon"]
I want to take each element with the same index in each list and combine them into a resulting list like the below:
["1 Ham", "2 Cheese","3 Bacon"]
Where either list can be empty.
Solution 1:[1]
I tried the below and it worked for me:
var list1= [1,2,3];
var list2=["Ham","Cheese","Bacon"];
var list3=list1.Count>0? list1.Zip(list2??Enumerable.Empty<string>(),(first,second)=> first+ " "+ second).ToList():list2;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Meche-Ann |
