Given are two IEnumerable<A> a
and IEnumerable<B> b
. It is guaranteed that they are of the same length. I would like to create a new IEnumerable<C> c
in which each item c_i
is derived using a Func<A, B, C> f
by c_i := f (a_i, b_i)
.
The best I could come up with is manual simultaneous enumeration over both sources and yield-ing the current result, implemented as an extension method. Is there a short way to do it without custom code in .NET >= 4.0?
An alternative solution to the Zip extension method can be achieved using nested Select and SelectMany to flatten the result. The implementation should take only the elements with the same index in the list (not the cross product):
The output in this case is:
You can use Enumerable.Zip. Given a function
f(A, B)
, you would haveYou can use
Enumerable.Zip
.e.g.
Use
Zip
method.http://msdn.microsoft.com/en-us/library/dd267698.aspx