Can we use several variables in foreach

Is it possible to use multiple variables in foreach

foreach (var item1 in collection1;var items2 in collection2) { } 

I want to do this because I need to get two collections from the database and add both of them to the ComboBox.

+10
c # asp.net-mvc-4
source share
8 answers

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.

+12
source share

You can pin 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 coincide in the same position (for example, the first element from collection1 connects the first element collectcion2). It is quite effective.

+4
source share

No, you cannot use multiple variables in foreach in a loop. Check the link link to the language . What happens if each collection has a different number of items?

If you want to iterate over both collections, try using union:

 foreach (var item1 in collection1.Union(collection2)) { ... } 
+1
source share

Judging by your comments, I think what you're really trying to do is not get the Cartesian product of the two collections, but the [SQL] UNION two sets. You have two options:

  • Concat two collections:

     foreach(var items in collection1.Concat(collection2)) {} 
  • Just add them both separately, assuming you don't have to do anything by iterating (possibly the best / easiest):

     myComboBox.Items.AddRange(collection1); myComboBox.Items.AddRange(collection2); 

If, however, you need the Cartesian product n * m [SQL pseudo-code] collection1 CROSS JOIN collection2 , you must use two nested foreach statements:

 foreach(var item1 in collection1) foreach(var item2 in collection2) { } 

Or you can join them in LINQ and iterate over the combined collection:

 foreach(var items in (from i1 in collection1 from i2 in collection2 select Tuple.Create(i1, i2))) { } 
+1
source share

foreach is used to list the individual elements in a collection. So you can’t. You must use it one by one. It would be better to use:

 void myfunc() {} foreach(var item1 in collection1){myfunc();} foreach(var item2 in collection2){myfunc();} 

than

 foreach(var item1 in collection1) foreach(var item2 in collection2) { myfunc(); } 

This will work n * m times. If the previous example was executed only for n + m times.

+1
source share

Do you want to combine elements from one collection with the corresponding elements of another?

 foreach (var pair in col1.Zip(col2, (Item1, Item2) => new { Item1, Item2 }) { //do something with pair.Item1 and pair.Item2 } 

Note: if the first collection has 10 elements and the second has 8, you will get 8 pairs; the last two elements in the first collection will be discarded because in the second collection they will not match. In general, the number of iterations will be Min(col1.Count(), col2.Count()) .

Do you want to iterate over all the elements in one collection, and then all the elements in the second?

 foreach (var element in col1.Concat(col2)) { //do something with element } 

Note: if the first collection has 10 elements and the second has 8, this loop will be executed 18 times or, more generally, the number of iterations will be col1.Count() + col2.Count() .

Do you want to associate each item in one collection with each item in another?

 foreach (var item1 in col1) foreach (var item2 in col2) { //do something with item1 and item2 } 

Note: this is a Cartesian product, so it is not surprising that the number of iterations is a product of the size of the collections. If we have 10 and 8 elements, the loop will execute 80 times. To ensure consistency, this is col1.Count() * col2.Count() .

0
source share

You can use an iterator:

 IEnumerator <Item2Type> item2Itt = Collection2.GetEnumerator(); item2Itt.MoveNext(); // The iterator returned above is BEFORE the first element in the collection. foreach (Item1Type item1 in collection1) { item1.blahblahblah; item2Itt.Current.blahBlahBlah; item2Itt.MoveNext(); } 
0
source share

I think this method can be used:

 List<Type> allOfThem = new List<Type>(); //use proper type for collection allOfThem.AddRange(collection1); allOfThem.AddRange(collection2); foreach (Type item in allOfThem) { ... } 
-one
source share

All Articles