LINQ Cross Join

In LINQ, Cross join will produce the Cartesian products of collection items, and we don't need any conditions to join collections.

 

In LINQ Cross join, each element in the left side collection will be mapped to all the elements in the right side collection.

Syntax of LINQ Cross Join

Following is the syntax of using LINQ Cross join to get a Cartesian product of collection items.

 

C# Code

 

var result = from e in objEmp
             from d in objDept
             select new
             {
               EmployeeName = e.Name,
               DepartmentName = d.DepName
             };

VB.NET Code

 

Dim result = From e In objEmp From d In objDept
Select New With {.EmployeeName = e.Name, .DepartmentName = d.DepName}

If you observe the above syntax each element in the “objEmp” collection will be mapped to all the elements in the “objDept” collection.

Example of LINQ Cross Join

Following is the example of using LINQ Cross Join to get Cartesian production of collection items.

 

C# Code

 

using System;
using System.Collections.Generic;
using System.Linq;

namespace Linqtutorials
{
  class Program
  {
    static void Main(string[] args)
    {
      List<Department> objDept = new List<Department>(){
        new Department{DepId=1,DepName="Software"},
        new Department{DepId=2,DepName="Finance"},
        new Department{DepId=3,DepName="Health"}
      };
      List<Employee> objEmp = new List<Employee>()
      {
         new Employee { EmpId=1,Name = "Suresh Dasari", DeptId=1 },
         new Employee { EmpId=2,Name = "Rohini Alavala", DeptId=1 },
         new Employee { EmpId=3,Name = "Praveen Kumar", DeptId=2 },
         new Employee { EmpId=4,Name = "Sateesh Chandra", DeptId =2},
         new Employee { EmpId=5,Name = "Madhav Sai"}
      };
      var result = from e in objEmp
                   from d in objDept
                   select new
                   {
                     EmployeeName = e.Name,
                     DepartmentName = d.DepName
                   };
      foreach (var item in result)
      {
        Console.WriteLine(item.EmployeeName + "\t | " + item.DepartmentName);
      }
      Console.ReadLine();
    }
  }
  class Department
  {
    public int DepId { get; set; }
    public string DepName { get; set; }
  }
  class Employee
  {
    public int EmpId { get; set; }
    public string Name { get; set; }
    public int DeptId { get; set; }
  }
}

VB.NET Code

 

Module Module1

Sub Main()
Dim objDept As New List(Of Department)() From {
New Department With {.DepId = 1, .DepName = "Software"},
New Department With {.DepId = 2, .DepName = "Finance"},
New Department With {.DepId = 3, .DepName = "Health"}
}
Dim objEmp As New List(Of Employee)() From {
New Employee With {.EmpId = 1, .Name = "Suresh Dasari", .DeptId = 1},
New Employee With {.EmpId = 2, .Name = "Rohini Alavala", .DeptId = 1},
New Employee With {.EmpId = 3, .Name = "Praveen Kumar", .DeptId = 2},
New Employee With {.EmpId = 4, .Name = "Sateesh Chandra", .DeptId = 2},
New Employee With {.EmpId = 5, .Name = "Madhav Sai"}
}
Dim result = From e In objEmp From d In objDept
Select New With {.EmployeeName = e.Name, .DepartmentName = d.DepName}
For Each item In result
Console.WriteLine(item.EmployeeName + vbTab & " | " + item.DepartmentName)
Next
Console.ReadLine()
End Sub
Class Department
Public Property DepId() As Int32
Get
Return m_DepId
End Get
Set(ByVal value As Int32)
m_DepId = value
End Set
End Property
Private m_DepId As Int32
Public Property DepName() As String
Get
Return m_DepName
End Get
Set(ByVal value As String)
m_DepName = value
End Set
End Property
Private m_DepName As String
End Class

Class Employee
Public Property EmpId() As Int32
Get
Return m_EmpId
End Get
Set(ByVal value As Int32)
m_EmpId = value
End Set
End Property
Private m_EmpId As Int32
Public Property Name() As String
Get
Return m_Name
End Get
Set(ByVal value As String)
m_Name = value
End Set
End Property
Private m_Name As String
Public Property DeptId() As String
Get
Return m_DeptId
End Get
Set(ByVal value As String)
m_DeptId = value
End Set
End Property
Private m_DeptId As String
End Class
End Module

If you observe the above example, we didn't mention any condition to join collections.

Result of LINQ Cross Join Example

Following is the result LINQ Cross Join example.

 

Suresh Dasari        | Software
Suresh Dasari        | Finance
Suresh Dasari        | Health
Rohini Alavala       | Software
Rohini Alavala       | Finance
Rohini Alavala       | Health
Praveen Kumar        | Software
Praveen Kumar        | Finance
Praveen Kumar        | Health
Sateesh Chandra      | Software
Sateesh Chandra      | Finance
Sateesh Chandra      | Health
Madhav Sai           | Software
Madhav Sai           | Finance
Madhav Sai           | Health

This is how we can use LINQ Cross join to get Cartesian production of collection items.