Use LINQ to join arrays by placing the result in an anonymous type, then move on to the resulting collection.
var col = collection1.Join(collection2, x => x, y => y, (x, y) => new { X = x, Y = y }); foreach (var entry in col) { // entry.X, entry.Y }
Edit:
When sending the response, I assumed that collection1 and collection2 contain different types. If they contain the same type or have a common base type, there are alternatives:
If you want to allow duplicates:
collection1.Concat(collection2); // same type collection1.select(x => (baseType)x).Concat(collection2.select(x => (baseType)x)); // shared base type
No duplicates:
collection1.Union(collection2); // same type collection1.select(x => (baseType)x).Union(collection2.select(x => (baseType)x)); // shared base type
Zip forms 4.0 forward framework can replace the original solution:
collection1.Zip(collection2, (x, y) => new { X = x, Y = y });
For an overview of most of the available LINQ functions, see 101 LINQ Samples .
Without LINQ, use two hierarchical foreach loops (increase the number of interactions) or one foreach loop to create the inermediate type, and the second one to iterate over the set of intermediate elements or if the types in the collections are the same, add them to (using AddRange) and then repeat this new one list.
Many roads lead to one goal ... its up to you to choose it.
Axelckenberger
source share