Mega Code Archive

 
Categories / C# Book / 04 LINQ
 

0520 Combine

public static class CustomSequenceOperators { public static IEnumerable Combine(this IEnumerable first, IEnumerable second, Func func) { using (IEnumerator e1 = first.GetEnumerator(), e2 = second.GetEnumerator()) { while (e1.MoveNext() && e2.MoveNext()) { yield return func(e1.Current, e2.Current); } } } } public void Linq98() { int[] vectorA = { 0, 2, 4, 5, 6 }; int[] vectorB = { 1, 3, 5, 7, 8 }; int dotProduct = vectorA.Combine(vectorB, (a, b) => a * b).Sum(); Console.WriteLine("Dot product: {0}", dotProduct); } Result Dot product: 109