Can we use multiple variables in foreach
foreach (var item1 in collection1;var items2 in collection2)
{
}
I want to do this because I need to fetch two collections from a database and append both of them to a ComboBox.
forEach() accepts up to three arguments: the current element in the array upon which . forEach() was called, the index of that element in the array, and the array itself.
Output. Note − If there are more than 2 arrays, nested 'foreach' loops can be used. Here, 2 arrays have been declared and they are being traversed using the 'foreach' loop. The result is that the respective index of every array is matched and the data at those indices are displayed one next to the other.
This foreach loop is faster because the local variable that stores the value of the element in the array is faster to access than an element in the array. The forloop is faster than the foreach loop if the array must only be accessed once per iteration.
An important feature of foreach is the %:% operator. I call this the nesting operator because it is used to create nested foreach loops. Like the %do% and %dopar% operators, it is a binary operator, but it operates on two foreach objects.
Use LINQ to join the arrays putting the result into an anonymous type, then iterate over 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 posting the answer I assumed that collection1
and collection2
contained different types. If they contain both the same type or share 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
Form framework 4.0 onwards Zip can replace the original solution:
collection1.Zip(collection2, (x, y) => new { X = x, Y = y });
For an overview over most of the available LINQ funktions please refer to 101 LINQ Samples.
Without LINQ use two hierarchical foreach loops (increasing the number of interations) or one foreach loop to create an inermediate type and a second to iterate over the collection of intermediates or if the types in the collections are the same add them to a list (using AddRange)
and then iterate over this new list.
Many roads lead to one goal ... its up to you to chose one.
You can Zip the collections
foreach (var item in collection1.Zip(collection2, (a, b) => new { A = a, B = b }))
{
var a = item.A;
var b = item.B;
// ...
}
This assumes that the elements match at the same position (e.g. the first element from collection1 joins the first element of collecion2). It is quite efficient.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With