Part of the 101 LINQ SAMPLES
Learn how to use LINQ in your applications with these code samples, covering the entire range of LINQ functionality and demonstrating LINQ with SQL, DataSets, and XML.


This sample shows different uses of Custom Sequence Operators:

Building the Sample

  1. Open the Program.cs
  2. Comment or uncomment the desired samples
  3. Press Ctrl + F5



(back to top)

This sample calculates the dot product of two integer vectors. It uses a user-created sequence operator, Combine, to calculate the dot product, passing it a lambda function to multiply two arrays, element by element, and sum the result.

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 = { 02456 }; 
    int[] vectorB = { 13578 }; 
    int dotProduct = vectorA.Combine(vectorB, (a, b) => a * b).Sum(); 
    Console.WriteLine("Dot product: {0}", dotProduct); 


Dot product: 109


Source Code Files

101 LINQ Samples

More Information

For more information, see: