Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to iterate through two IEnumerables simultaneously?

I have two enumerables: IEnumerable<A> list1 and IEnumerable<B> list2. I would like to iterate through them simultaneously like:

foreach((a, b) in (list1, list2))
{
    // use a and b
}

If they don't contain the same number of elements, an exception should be thrown.

What is the best way to do this?

like image 590
Danvil Avatar asked Apr 27 '10 14:04

Danvil


4 Answers

You want something like the Zip LINQ operator - but the version in .NET 4 always just truncates when either sequence finishes.

The MoreLINQ implementation has an EquiZip method which will throw an InvalidOperationException instead.

var zipped = list1.EquiZip(list2, (a, b) => new { a, b });

foreach (var element in zipped)
{
    // use element.a and element.b
}
like image 130
Jon Skeet Avatar answered Nov 01 '22 01:11

Jon Skeet


Here's an implementation of this operation, typically called Zip:

using System;
using System.Collections.Generic;

namespace SO2721939
{
    public sealed class ZipEntry<T1, T2>
    {
        public ZipEntry(int index, T1 value1, T2 value2)
        {
            Index = index;
            Value1 = value1;
            Value2 = value2;
        }

        public int Index { get; private set; }
        public T1 Value1 { get; private set; }
        public T2 Value2 { get; private set; }
    }

    public static class EnumerableExtensions
    {
        public static IEnumerable<ZipEntry<T1, T2>> Zip<T1, T2>(
            this IEnumerable<T1> collection1, IEnumerable<T2> collection2)
        {
            if (collection1 == null)
                throw new ArgumentNullException("collection1");
            if (collection2 == null)
                throw new ArgumentNullException("collection2");

            int index = 0;
            using (IEnumerator<T1> enumerator1 = collection1.GetEnumerator())
            using (IEnumerator<T2> enumerator2 = collection2.GetEnumerator())
            {
                while (enumerator1.MoveNext() && enumerator2.MoveNext())
                {
                    yield return new ZipEntry<T1, T2>(
                        index, enumerator1.Current, enumerator2.Current);
                    index++;
                }
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            int[] numbers = new[] { 1, 2, 3, 4, 5 };
            string[] names = new[] { "Bob", "Alice", "Mark", "John", "Mary" };

            foreach (var entry in numbers.Zip(names))
            {
                Console.Out.WriteLine(entry.Index + ": "
                    + entry.Value1 + "-" + entry.Value2);
            }
        }
    }
}

To make it throw an exception if just one of the sequences run out of values, change the while-loop so:

while (true)
{
    bool hasNext1 = enumerator1.MoveNext();
    bool hasNext2 = enumerator2.MoveNext();
    if (hasNext1 != hasNext2)
        throw new InvalidOperationException("One of the collections ran " +
            "out of values before the other");
    if (!hasNext1)
        break;

    yield return new ZipEntry<T1, T2>(
        index, enumerator1.Current, enumerator2.Current);
    index++;
}
like image 37
Lasse V. Karlsen Avatar answered Nov 01 '22 01:11

Lasse V. Karlsen


In short, the language offers no clean way to do this. Enumeration was designed to be done over one enumerable at a time. You can mimic what foreach does for you pretty easily:

using(IEnumerator<A> list1enum = list1.GetEnumerator())
using(IEnumerator<B> list2enum = list2.GetEnumerator())    
while(list1enum.MoveNext() && list2enum.MoveNext()) {
        // list1enum.Current and list2enum.Current point to each current item
    }

What to do if they are of different length is up to you. Perhaps find out which one still has elements after the while loop is done and keep working with that one, throw an exception if they should be the same length, etc.

like image 22
Matt Greer Avatar answered Oct 31 '22 23:10

Matt Greer


In .NET 4, you can use the .Zip extension method on IEnumerable<T>

IEnumerable<int> list1 = Enumerable.Range(0, 100);
IEnumerable<int> list2 = Enumerable.Range(100, 100);

foreach (var item in list1.Zip(list2, (a, b) => new { a, b }))
{
    // use item.a and item.b
}

It won't throw on unequal lengths, however. You can always test that, though.

like image 7
Anthony Pegram Avatar answered Nov 01 '22 01:11

Anthony Pegram