maf*_*afu 2 .net c# linq enumeration
鉴于是两个IEnumerable<A> a和IEnumerable<B> b.保证它们具有相同的长度.我想创建一个新的IEnumerable<C> c,其中每个项目c_i使用Func<A, B, C> fby 派生c_i := f (a_i, b_i).
我能想到的最好的是两个源的手动同时枚举,并产生当前结果,作为扩展方法实现.如果没有.NET> = 4.0中的自定义代码,有没有简短的方法呢?
你可以用Enumerable.Zip.
例如
var c = a.Zip(b, (a, b) => SomeFunc(a, b));
Run Code Online (Sandbox Code Playgroud)
使用Zip方法.
http://msdn.microsoft.com/en-us/library/dd267698.aspx
将指定的函数应用于两个序列的相应元素,生成一系列结果.
int[] numbers = { 1, 2, 3, 4 };
string[] words = { "one", "two", "three" };
var numbersAndWords = numbers.Zip(words, (first, second) => first + " " + second);
foreach (var item in numbersAndWords)
Console.WriteLine(item);
// This code produces the following output:
// 1 one
// 2 two
// 3 three
Run Code Online (Sandbox Code Playgroud)