Working With Cross Join Using LINQ And Lambda

Here, you will learn how to work with cross join using Linq and Lambda

Cross Join or Cartesian product
 
When combining two sequences using this process, every item in the first collection is combined with every item in the second one. No key selection is required as there is no filtering of data. The resultant sequence will always have a number of items equal to the product of the sizes of the two source sequences.
 
SQL Syntax 
  1. SELECT table1.column1, table2.column2...  
  2. FROM table1, table2 [, table3 ]-  
Okay! Now, let us see the example in both LINQ and lambda. For that, I have created two classes and added dummy values to it.
  1. class Customer {  
  2.     public int CustId {  
  3.         get;  
  4.         set;  
  5.     }  
  6.     public string Name {  
  7.         get;  
  8.         set;  
  9.     }  
  10.     public string Address {  
  11.         get;  
  12.         set;  
  13.     }  
  14.     public string PhoneNumber {  
  15.         get;  
  16.         set;  
  17.     }  
  18. }  
  19. class Car {  
  20.     public int CarId {  
  21.         get;  
  22.         set;  
  23.     }  
  24.     public string ModelName {  
  25.         get;  
  26.         set;  
  27.     }  
  28.     public string Color {  
  29.         get;  
  30.         set;  
  31.     }  
  32.     public int SoldTo {  
  33.         get;  
  34.         set;  
  35.     }  
  36. }  
  37. Customer[] customers = new Customer[] {  
  38.     new Customer {  
  39.         CustId = 101, Name = "Charlie C", Address = "USA", PhoneNumber = "042-548685"  
  40.     },  
  41.     new Customer {  
  42.         CustId = 102, Name = "Bean ", Address = "UK", PhoneNumber = "043-48521"  
  43.     },  
  44.     new Customer {  
  45.         CustId = 103, Name = "Albert", Address = "Germany", PhoneNumber = "044-5445522"  
  46.     }  
  47. };  
  48. Car[] cars = new Car[] {  
  49.     new Car {  
  50.         CarId = 4251, Color = "Black", ModelName = "Ambassador", SoldTo = 101  
  51.     },  
  52.     new Car {  
  53.         CarId = 4252, Color = "White", ModelName = "Audi", SoldTo = 102  
  54.     },  
  55.     new Car {  
  56.         CarId = 4253, Color = "Silver", ModelName = "BMW", SoldTo = 103  
  57.     },  
  58. };  
The collection values are shown below.

 

The following sample code demonstrates a cross join query using Lambda.
  1. #region CrossJoin using Lambda Query  
  2. //defered query execution  
  3. var crossJoinLambda = customers.SelectMany(t1 => cars.Select(t2 => new {  
  4.     CustomerName = t1.Name,  
  5.         PhoneNumber = t1.PhoneNumber,  
  6.         CarName = t2.ModelName,  
  7. }));  
  8. //Immediate query execution  
  9. foreach(var custmerData in crossJoinLambda) {  
  10.     Console.WriteLine("CustomerName :" + custmerData.CustomerName + " , PhoneNumber : " + custmerData.PhoneNumber + " , CarName : " + custmerData.CarName);  
  11. }#endregion  
Result

 

The following sample code demonstrates a cross join query using LINQ.
  1. #region CrossJoin using Linq Query  
  2. //defered query execution  
  3. var crossJoinLinq = from customer in customers  
  4. from car in cars  
  5. //where customer.CustId == car.SoldTo  
  6. select new {  
  7.     CustomerName = customer.Name, PhoneNumber = customer.PhoneNumber, CarName = car.ModelName  
  8. };  
  9. //Immediate query execution  
  10. foreach(var custmerData in crossJoinLinq) {  
  11.     Console.WriteLine("CustomerName :" + custmerData.CustomerName + " , PhoneNumber : " + custmerData.PhoneNumber + " , CarName : " + custmerData.CarName);  
  12. }#endregion  
Result

 
 
I hope you got the clear picture about cross join.
 
Complete Code 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. namespace CrossJoinINLinqAndLambda {  
  7.     class Program {  
  8.         static void Main(string[] args) {  
  9.             Customer[] customers = new Customer[] {  
  10.                 new Customer {  
  11.                     CustId = 101, Name = "Charlie C", Address = "USA", PhoneNumber = "042-548685"  
  12.                 },  
  13.                 new Customer {  
  14.                     CustId = 102, Name = "Bean ", Address = "UK", PhoneNumber = "043-48521"  
  15.                 },  
  16.                 new Customer {  
  17.                     CustId = 103, Name = "Albert", Address = "Germany", PhoneNumber = "044-5445522"  
  18.                 }  
  19.             };  
  20.             Car[] cars = new Car[] {  
  21.                 new Car {  
  22.                     CarId = 4251, Color = "Black", ModelName = "Ambassador", SoldTo = 101  
  23.                 },  
  24.                 new Car {  
  25.                     CarId = 4252, Color = "White", ModelName = "Audi", SoldTo = 102  
  26.                 },  
  27.                 new Car {  
  28.                     CarId = 4253, Color = "Silver", ModelName = "BMW", SoldTo = 103  
  29.                 },  
  30.             };#  
  31.             region Customer and Car Collection  
  32.             Console.WriteLine("Customer Collection \n\t");  
  33.             foreach(var customer in customers) {  
  34.                 Console.WriteLine("ID : " + customer.CustId + " , Name : " + customer.Name + " , Address : " + customer.Address + " , PhoneNo : " + customer.PhoneNumber);  
  35.             }  
  36.             Console.WriteLine("\n Car Collection \n\t");  
  37.             foreach(var car in cars) {  
  38.                 Console.WriteLine("ID : " + car.CarId + " , Name : " + car.ModelName + " , Name : " + car.Color + " , Name : " + car.SoldTo);  
  39.             }#  
  40.             endregion  
  41.             Console.WriteLine("\n\t CrossJoin using Linq Query \n\t");#  
  42.             region CrossJoin using Linq Query  
  43.             //defered query execution  
  44.             var crossJoinLinq = from customer in customers  
  45.             from car in cars  
  46.             //where customer.CustId == car.SoldTo  
  47.             select new {  
  48.                 CustomerName = customer.Name, PhoneNumber = customer.PhoneNumber, CarName = car.ModelName  
  49.             };  
  50.             //Immediate query execution  
  51.             foreach(var custmerData in crossJoinLinq) {  
  52.                 Console.WriteLine("CustomerName :" + custmerData.CustomerName + " , PhoneNumber : " + custmerData.PhoneNumber + " , CarName : " + custmerData.CarName);  
  53.             }#  
  54.             endregion  
  55.             Console.WriteLine("\n\t CrossJoin using Lambda Query \n\t");#  
  56.             region CrossJoin using Lambda Query  
  57.             //defered query execution  
  58.             var crossJoinLambda = customers.SelectMany(t1 => cars.Select(t2 => new {  
  59.                 CustomerName = t1.Name,  
  60.                     PhoneNumber = t1.PhoneNumber,  
  61.                     CarName = t2.ModelName,  
  62.             }));  
  63.             //Immediate query execution  
  64.             foreach(var custmerData in crossJoinLambda) {  
  65.                 Console.WriteLine("CustomerName :" + custmerData.CustomerName + " , PhoneNumber : " + custmerData.PhoneNumber + " , CarName : " + custmerData.CarName);  
  66.             }#  
  67.             endregion  
  68.         }  
  69.     }  
  70.     class Customer {  
  71.         public int CustId {  
  72.             get;  
  73.             set;  
  74.         }  
  75.         public string Name {  
  76.             get;  
  77.             set;  
  78.         }  
  79.         public string Address {  
  80.             get;  
  81.             set;  
  82.         }  
  83.         public string PhoneNumber {  
  84.             get;  
  85.             set;  
  86.         }  
  87.     }  
  88.     class Car {  
  89.         public int CarId {  
  90.             get;  
  91.             set;  
  92.         }  
  93.         public string ModelName {  
  94.             get;  
  95.             set;  
  96.         }  
  97.         public string Color {  
  98.             get;  
  99.             set;  
  100.         }  
  101.         public int SoldTo {  
  102.             get;  
  103.             set;  
  104.         }  
  105.     }  
  106. }  
Refer to the attached demo project

I hope it's helpful.