如何可以将两个枚举用自定义的功能? [英] How can combine two enumerations with a custom function?
问题描述
由于两个的IEnumerable< A>一个
和的IEnumerable< B> b
。这保证它们是相同的长度。我想创建一个新的的IEnumerable< C> ç
,其中每个项目 C_I
使用 Func键&LT推导; A,B,C> ˚F
按 C_I:= F(A_I,b_i)
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)
.
我能想出的最好的是手动的同时列举了两个电源和产量荷兰国际集团目前的结果,实现为一个扩展方法。有一个简短的方式来做到这一点,而不在.NET自定义code> = 4.0?
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?
推荐答案
您可以使用 Enumerable.Zip
。
You can use Enumerable.Zip
.
例如。
var c = a.Zip(b, (a, b) => SomeFunc(a, b));
这篇关于如何可以将两个枚举用自定义的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!