Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Linq enumerate multiple collections simultaneously
public static class EnumerableExtensions
/// <summary>
/// Enumerates over two sequences simulateneously and performs the action on both
/// </summary>
/// <typeparam name="T1"></typeparam>
/// <typeparam name="T2"></typeparam>
/// <param name="source1"></param>
/// <param name="source2"></param>
/// <param name="eacher">The action which takes a value from each sequence</param>
public static void ZipEach<T1, T2>(this IEnumerable<T1> source1, IEnumerable<T2> source2, Action<T1, T2> action)
var source1Enumerator = source1.GetEnumerator();
var source2Enumerator = source2.GetEnumerator();
for (; AdvanceEach(source1Enumerator, source2Enumerator);)
eacher(source1Enumerator.Current, source2Enumerator.Current);
private static bool AdvanceEach(params System.Collections.IEnumerator [] enumerators)
return enumerators.All(e => e.MoveNext());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment