---
title: "LINQ Joins in C#"  
description: "In this blog, I’m explaining about joins in LINQ in .NetThere are different types of LINQ joins.1.Inner join2.Left join3.Cross join4.Group joinInner j"  
author: "priyanka kushwaha"  
published: 2015-01-28  
updated: 2015-01-28  
canonical: https://www.mindstick.com/blog/734/linq-joins-in-c-sharp  
category: ".net"  
tags: [".net", "linq"]  
reading_time: 3 minutes  

---

# LINQ Joins in C#

In this blog, I’m explaining about joins in LINQ in .Net

##### There are different types of LINQ joins.

1. [Inner join](https://www.mindstick.com/forum/33572/sql-inner-join-keyword)

2. [Left join](https://www.mindstick.com/blog/304465/difference-between-inner-join-left-join-right-join-and-full-outer-join-in-sql-server)

3. [Cross join](https://www.mindstick.com/interview/1850/what-is-cross-join)

4. Group join

##### Inner join

Inner join returns only those [records](https://www.mindstick.com/forum/34640/how-to-create-a-stored-procedure-for-display-all-records) or rows that match or exists in both the [tables](https://www.mindstick.com/articles/336597/introduction-of-html-tables-for-web-development).

##### Example:

```
var innerjoin = (from d in db.EmployeeDetails join rl in db.RoleTables on d.RoleId equals rl.RoleId             select new { d.id, d.FirstName, rl.RoleName, rl.RoleId }).ToList();
```

##### Left join

Left join returns all records or rows from left table from right table returns only match records. If there are no columns [matching](https://www.mindstick.com/forum/162034/what-is-semantic-matching) in the right table it [returns NULL](https://www.mindstick.com/forum/159370/sql-join-returns-null-records-when-there-is-no-data-in-the-database-in-sql-server-why) values. It is mandatory to use “INTO” keyword and “[DefaultIFEmpty](https://www.mindstick.com/forum/161158/how-to-use-defaultifempty-function-in-linq-with-c-sharp)()” method.

##### Example:

```
var emp = (from d in db.EmployeeDetails join rl in db.RoleTables on d.RoleId equals rl.RoleId into t from lj in t.DefaultIfEmpty()                                     select new { d.id, d.FirstName, roleid = (int?)d.RoleId, lj.RoleName }).ToList();
```

##### Cross Join

##### \

Cross join is a Cartesian join means Cartesian product of both the tables.

This join does not need any condition to join two tables. This join returns

records or rows that are multiplication of record number from both the

tables means each row on left table will related to each row of right table.

##### Example:

```
var cjoin = (from d in db.EmployeeDetails from o in db.RoleTables select new { d.id, d.FirstName, o.RoleId, o.RoleName }).ToList();
```

##### Group Join

When a join use a “INTO” [expression](https://www.mindstick.com/articles/1861/sqlite-expressions), then it is called a group join.

##### Example:

```
var gjoin = (from d in db.RoleTables join o in db.EmployeeDetails on d.RoleId equals o.RoleId group d by new { d.RoleId, d.RoleName } into grp orderby grp.Count() select new { grp.Key.RoleId, grp.Key.RoleName, count = grp.Count() }).ToList();
```

##### Example

```
namespace LINQTask{    class Program    {        static void Main(string[] args)        {         DataClasses1DataContext db = new DataClasses1DataContext();          //inner joining            Console.WriteLine("Inner joining");            var innerjoin = (from d in db.EmployeeDetails join rl in db.RoleTables on d.RoleId equals rl.RoleId select new { d.id, d.FirstName, rl.RoleName, rl.RoleId }).ToList();            foreach (var d in innerjoin)            {                Console.WriteLine("Employee Id:{0},Name:{1},Role Id:{2},Role Name:{3}", d.id, d.FirstName.Trim(), d.RoleId, d.RoleName);            }            //Left joining            Console.WriteLine("Left Joining");            var emp = (from d in db.EmployeeDetails join rl in db.RoleTables on d.RoleId equals rl.RoleId into t from lj in t.DefaultIfEmpty() select new { d.id, d.FirstName, roleid = (int?)d.RoleId, lj.RoleName }).ToList();            foreach (var d in emp)            {                Console.WriteLine("Employee Id:{0},Name:{1},Role Id:{2},Role Name:{3}", d.id, d.FirstName.Trim(), d.roleid, d.RoleName);            }            //Cross joining            Console.WriteLine("Cross Joining");            var cjoin = (from d in db.EmployeeDetails from o in db.RoleTables select new { d.id, d.FirstName, o.RoleId, o.RoleName }).ToList();            foreach (var cobj in cjoin)            {                Console.WriteLine("Employee Id:{0},Name:{1},Role Id:{2},Role Name:{3}", cobj.id, cobj.FirstName.Trim(), cobj.RoleId, cobj.RoleName);            }             //Group join            Console.WriteLine("Group joining");            var gjoin = (from d in db.RoleTables join o in db.EmployeeDetails on d.RoleId equals o.RoleId group d by new { d.RoleId, d.RoleName } into grp orderby grp.Count() select new { grp.Key.RoleId, grp.Key.RoleName, count = grp.Count() }).ToList(); ;            foreach (var cobj in gjoin)                Console.WriteLine("Role Id:{0},Role Name:{1},count:{2}", cobj.RoleId, cobj.RoleName.Trim(), cobj.count);             Console.ReadKey();         }    }}
```

---

Original Source: https://www.mindstick.com/blog/734/linq-joins-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
