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


Introduction

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


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5. class LinqExercise23
  6. {
  7. public static void Main(string[] args)
  8. {
  9. char[] charlist = { 'A', 'B', 'C' };
  10. int[] numlist = { 1, 2, 3 };
  11.  
  12. var cartesianProduct = from letterlist in charlist
  13. from numberlist in numlist
  14. select new { letterlist, numberlist };
  15.  
  16. Console.Write("The Cartesian Product are : \n");
  17. foreach (var productItem in cartesianProduct)
  18. {
  19. Console.WriteLine(productItem);
  20. }
  21. Console.ReadLine();
  22. }
  23. }

Result

C# language techstudy.org

     





Related Topics




Popular Posts