LINQ Cross Join

  Total Views : 183
  Comments : 0
 
  Keywords : Linq cross join in c# / vb.net with example, How to use linq cross join c# / vb.net to get cartesian product of collection items with example, Linq cross join example in c# / vb.net

Here we will learn LINQ cross join in c# / vb.net with example and how to use LINQ cross join to get cartesian product of two collection items in c#, vb.net with example.

LINQ Cross Join

In LINQ Cross join will produce Cartesian product of collection items and we don't need any condition to join collections.

 

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

Syntax of LINQ Cross Join

Following is the syntax of using LINQ Cross join to get 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 above syntax each element in “objEmp” collection will be mapped to all the elements in “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 above example we didn't mentioned any condition to join collections.

Output 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.

 
 

NEXT

LINQ Group Join  
 

 Comments (0)

Be the first to give your valuable feedback

Leave a comment

  • Default Tutlane User