Q:

Write a program in C# to generate a Cartesian Product of two sets in LINQ Query

belongs to collection: All LINQ programs in C# with examples

0

Write a program in C# to generate a Cartesian Product of two sets in LINQ Query

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

I have used Visual Studio 2012 for debugging purpose. But you can use any version of visul studio as per your availability.

using System;
using System.Linq;
using System.Collections.Generic;
 
class LinqExercise23
{
    public static void Main(string[] args)
    {
        char[] charlist = { 'A', 'B', 'C' };
        int[] numlist = { 1, 2, 3 };
 
        var cartesianProduct = from letterlist in charlist
                               from numberlist in numlist
                               select new { letterlist, numberlist };
 
        Console.Write("The Cartesian Product are : \n");
        foreach (var productItem in cartesianProduct)
        {
            Console.WriteLine(productItem);
        }
        Console.ReadLine();
    }
}

Result:

The Cartesian Product are : 

{ letterlist = A, numberlist = 1 }

{ letterlist = A, numberlist = 2 }

{ letterlist = A, numberlist = 3 }

{ letterlist = B, numberlist = 1 }

{ letterlist = B, numberlist = 2 }

{ letterlist = B, numberlist = 3 }

{ letterlist = C, numberlist = 1 }

{ letterlist = C, numberlist = 2 }

{ letterlist = C, numberlist = 3 }

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Write a program in C# to arrange the distinct elem... >>
<< Write a program in C# to remove a range of items f...