LINQ to SQL Cross Join

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

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

LINQ to SQL Cross Join

In LINQ to SQL the cross join will produce Cartesian product of both the collection items. In cross join we don't not need to write any condition to join two tables and it produces multiplication of record number from both the tables that means each row on left table will relate to each row of right table.

Syntax of LINQ to SQL Cross Join

Following is the syntax of using LINQ to SQL Cross Join to get Cartesian product of collections.

 

C# Code

 

var result = from ed in db.EmployeeDetails

from d in db.Departments

select new

{

Name = ed.EmpName,

Department = d.DeptName

};

VB.NET Code

 

Dim result = From ed In db.EmployeeDetails From d In db.Departments Select New With {.Name = ed.EmpName, .Department = d.DeptName}

If you observe above syntax we used “EmployeeDetails”, “Departments” tables to get Cartesian product of data without using any join.

Example of LINQ to SQL Cross Join

Before we start implement LINQ to SQL cross join example first we need to create a database with required tables and map those tables to LINQ to SQL file (.dbml) if you don't know the process don't worry check this link create and map database tables to LINQ to SQL file (.dbml).

 

Once we create and map required tables to .dbml file now we will show data in our application for that Right click on application → select Add → New Item → Select Web Form → Give name as Default.aspx and click OK button.

 

Now open Default.aspx page and write the code like as shown below

 

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

<title>Show Employee Details in Gridview</title>

</head>

<body>

<form id="form1" runat="server">

<div>

<asp:GridView ID="gvDetails" runat="server"></asp:GridView>

</div>

</form>

</body>

</html>

Now open code behind file and write the code like as shown below

 

C# Code

 

protected void Page_Load(object sender, EventArgs e)

{

EmployeeDBDataContext db = new EmployeeDBDataContext();

if (!Page.IsPostBack)

{

var result = from ed in db.EmployeeDetails

from d in db.Departments

select new

{

Name = ed.EmpName,

Location = ed.Location,

Gender = ed.Gender,

Department = d.DeptName

};

gvDetails.DataSource = result;

gvDetails.DataBind();

}

}

VB.NET Code

 

Public Class WebForm1

Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Dim db As New EmployeeDBDataContext()

If Not Page.IsPostBack Then

Dim result = From ed In db.EmployeeDetails From d In db.Departments Select New With {.Name = ed.EmpName, .Location = ed.Location, .Gender = ed.Gender, .Department = d.DeptName}

gvDetails.DataSource = result

gvDetails.DataBind()

End If

End Sub

End Class

If you observe above example, we are getting data from “EmployeeDetails” and “Departments” table without using any join.

Output of LINQ to SQL Cross Join Example

Following is the result of LINQ to SQL cross join example.

 

LINQ to SQL Cross Join Example Result

 

This is how we can use LINQ to SQL cross join in c#, vb.net to get cartesian product of data from multiple tables based on our requirements.

 Comments (0)

Be the first to give your valuable feedback

Leave a comment

  • Default Tutlane User