Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to iterate through two collections of the same length using a single foreach

Tags:

c#

I know this question has been asked many times before but I tried out the answers and they don't seem to work.

I have two lists of the same length but not the same type, and I want to iterate through both of them at the same time as list1[i] is connected to list2[i].

Eg:

Assuming that i have list1 (as List<string>) and list2 (as List<int>)

I want to do something like

foreach( var listitem1, listitem2 in list1, list2)
{
   // do stuff
}

Is this possible?

like image 852
Dan Dinu Avatar asked Dec 15 '11 10:12

Dan Dinu


2 Answers

This is possible using .NET 4 LINQ Zip() operator or using open source MoreLINQ library which provides Zip() operator as well so you can use it in more earlier .NET versions

Example from MSDN:

int[] numbers = { 1, 2, 3, 4 };
string[] words = { "one", "two", "three" };

// The following example concatenates corresponding elements of the
// two input sequences.
var numbersAndWords = numbers.Zip(words, (first, second) => first + " " + second);
foreach (var item in numbersAndWords)
{
    Console.WriteLine(item);
}

// OUTPUT:
// 1 one
// 2 two
// 3 three

Useful links:

  • Soure code of the MoreLINQ Zip() implementation: MoreLINQ Zip.cs
like image 99
sll Avatar answered Oct 19 '22 14:10

sll


Edit - Iterating whilst positioning at the same index in both collections

If the requirement is to move through both collections in a 'synchronized' fashion, i.e. to use the 1st element of the first collection with the 1st element of the second collection, then 2nd with 2nd, and so on, without needing to perform any side effecting code, then see @sll's answer and use .Zip() to project out pairs of elements at the same index, until one of the collections runs out of elements.

More Generally

Instead of the foreach, you can access the IEnumerator from the IEnumerable of both collections using the GetEnumerator() method and then call MoveNext() on the collection when you need to move on to the next element in that collection. This technique is common when processing two or more ordered streams, without needing to materialize the streams.

var stream1Enumerator = stream1.GetEnumerator();
var stream2Enumerator = stream2.GetEnumerator();
var currentGroupId = -1; // Initial value
// i.e. Until stream1Enumerator runs out of 
while (stream1Enumerator.MoveNext())
{
   // Now you can iterate the collections independently
   if (stream1Enumerator.Current.Id != currentGroupId)
   {
       stream2Enumerator.MoveNext();
       currentGroupId = stream2Enumerator.Current.Id;
   }
   // Do something with stream1Enumerator.Current and stream2Enumerator.Current
}

As others have pointed out, if the collections are materialized and support indexing, such as an ICollection interface, you can also use the subscript [] operator, although this feels rather clumsy nowadays:

var smallestUpperBound = Math.Min(collection1.Count, collection2.Count);
for (var index = 0; index < smallestUpperBound; index++)
{
     // Do something with collection1[index] and collection2[index]
}

Finally, there is also an overload of Linq's .Select() which provides the index ordinal of the element returned, which could also be useful.

e.g. the below will pair up all elements of collection1 alternatively with the first two elements of collection2:

var alternatePairs = collection1.Select(
    (item1, index1) => new 
    {
        Item1 = item1,
        Item2 = collection2[index1 % 2]
    });
like image 35
StuartLC Avatar answered Oct 19 '22 13:10

StuartLC