In this article, I will demonstrate how we can perform join operations with SQL database using Language-Integrated Query (LINQ).
- CREATE TABLE [dbo].[Employee](
- [ID] [int] IDENTITY(1,1) NOT NULL,
- [Name] [nvarchar](50) NULL,
- [Position] [nvarchar](50) NULL,
- [Office] [nvarchar](50) NULL,
- [Salary] [money] NULL,
- [DepartmentId] [int] NULL,
- CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED
- (
- [ID] ASC
- )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
- ) ON [PRIMARY]
-
- GO
- CREATE TABLE [dbo].[Department](
- [ID] [int] IDENTITY(1,1) NOT NULL,
- [Department_Name] [nvarchar](50) NULL,
- CONSTRAINT [PK_Department] PRIMARY KEY CLUSTERED
- (
- [ID] ASC
- )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
- ) ON [PRIMARY]
-
- GO
Step 2
Open Visual Studio 2015 and create a new console application with a meaningful name.
Step 3
Add Entity Framework now. For that, right click on Models folder, select Add, then select New Item, then click on it.
Screenshot for adding Entity Framework 1
After clicking on new item, you will get a window; from there, select Data from the left panel and choose ADO.NET Entity Data Model, give it the name DBModels (this name is not mandatory you can give any name) and click on Add.
Screenshot for adding Entity Framework 2
After you click on "Add a window", the wizard will open, choose EF Designer from the database and click next.
Screenshot for adding Entity Framework 3
After clicking on Next a window will appear. Choose New Connection. Another window will appear, add your server name if it is local then enter dot (.). Choose your database and click on OK.
Screenshot for adding Entity Framework 4
A connection will be added. You can change the name of your connection below. It will save connection in web config then click on Next.
Screenshot for adding Entity Framework 5
After clicking on NEXT another window will appear; choose database table name as shown in the below screenshot then click on Finish.
Screenshot for adding Entity Framework 6
Entity framework will be added and respective class gets generated under Models folder.
Screenshot for adding Entity Framework 7
Screenshot for adding Entity Framework 8
The following class will be added
Employee Class
- namespace JoinOperations_Demo
- {
- using System;
- using System.Collections.Generic;
-
- public partial class Employee
- {
- public int ID { get; set; }
- public string Name { get; set; }
- public string Position { get; set; }
- public string Office { get; set; }
- public Nullable<decimal> Salary { get; set; }
- public Nullable<int> DepartmentId { get; set; }
- }
- }
Department Class
- namespace JoinOperations_Demo
- {
- using System;
- using System.Collections.Generic;
-
- public partial class Department
- {
- public int ID { get; set; }
- public string Department_Name { get; set; }
- }
- }
Example of Group Join With Extension Method
- using System;
- using System.Linq;
-
- namespace JoinOperations_Demo
- {
- class GroupJoin
- {
- static void Main()
- {
- using (DBModel db=new DBModel())
- {
- var results = db.Departments.GroupJoin(db.Employees,
- d => d.ID,
- e => e.DepartmentId,
- (department, employee) => new
- {
- Department = department,
- Employee = employee
- });
-
- Console.WriteLine("----------GROUP JOIN----------");
- foreach (var department in results)
- {
- Console.WriteLine(" "+ department.Department.Department_Name);
-
- foreach (var employee in department.Employee)
- {
- Console.WriteLine(" \t"+ employee.Name);
- }
- }
- Console.ReadLine();
- }
- }
- }
- }
Output
Example of Group Join with SQL like syntax
- using System;
- using System.Linq;
-
- namespace JoinOperations_Demo
- {
- class GroupJoin
- {
- static void Main()
- {
- using (DBModel db=new DBModel())
- {
- var results = from d in db.Departments
- join e in db.Employees on
- d.ID equals e.DepartmentId into eGroup
- select new
- {
- Department = d,
- Employee = eGroup
- };
-
-
- Console.WriteLine("----------GROUP JOIN----------");
- foreach (var department in results)
- {
- Console.WriteLine(" "+ department.Department.Department_Name);
-
- foreach (var employee in department.Employee)
- {
- Console.WriteLine(" \t"+ employee.Name);
- }
- }
- Console.ReadLine();
- }
- }
- }
- }
Output
Example of Inner Join With Extension Method
- using System;
- using System.Linq;
-
- namespace JoinOperations_Demo
- {
- class InnerJoin
- {
- static void Main()
- {
- using (DBModel db=new DBModel())
- {
- var result = db.Employees.Join(db.Departments,
- e => e.DepartmentId,
- d => d.ID,
- (employee, department) => new
- {
- EmployeeName=employee.Name,
- DepartmentName=department.Department_Name
- });
-
- Console.WriteLine("----------INNER JOIN----------\n");
- foreach (var employee in result)
- {
- Console.WriteLine(" Name: " + employee.EmployeeName + "\t\t" + " Department: "+ employee.DepartmentName);
- }
- Console.ReadLine();
- }
- }
- }
- }
Output
Example of Inner Join with SQL like syntax
- using System;
- using System.Linq;
-
- namespace JoinOperations_Demo
- {
- class InnerJoin
- {
- static void Main()
- {
- using (DBModel db=new DBModel())
- {
- var result = from e in db.Employees
- join d in db.Departments on e.DepartmentId equals d.ID
- select new
- {
- EmployeeName=e.Name,
- DepartmentName=d.Department_Name
- };
-
- Console.WriteLine("----------INNER JOIN----------\n");
- foreach (var employee in result)
- {
- Console.WriteLine(" Name: " + employee.EmployeeName + "\t\t" + " Department: "+ employee.DepartmentName);
- }
- Console.ReadLine();
- }
- }
- }
- }
Output
Example of Left Outer Join With Extension Method
- using System;
- using System.Linq;
-
- namespace JoinOperations_Demo
- {
- class LeftOuterJoin
- {
- static void Main()
- {
- using (DBModel db=new DBModel())
- {
- var result = db.Employees.GroupJoin(db.Departments,
- e => e.DepartmentId, d => d.ID,(employee,department)=>new
- {
- EmployeeName=employee,
- DepartmentName =department
- })
- .SelectMany(x=>x.DepartmentName.DefaultIfEmpty(),
- (a,b)=>new
- {
- emp=a.EmployeeName.Name,
- dept=b==null? "No Department":b.Department_Name
- });
-
- Console.WriteLine("----------LEFT OUTER JOIN----------");
- foreach (var employee in result)
- {
- Console.WriteLine("Employee Name: " + employee.emp +"\t\t"+ "Department Name: " + employee.dept);
- }
-
- Console.ReadLine();
- }
- }
- }
- }
Output
Example of Left Outer Join with SQL like syntax
- using System;
- using System.Linq;
-
- namespace JoinOperations_Demo
- {
- class LeftOuterJoin
- {
- static void Main()
- {
- using (DBModel db=new DBModel())
- {
- var result = from e in db.Employees
- join d in db.Departments
- on e.DepartmentId equals d.ID into eGroup
- from d in eGroup.DefaultIfEmpty()
- select new
- {
- EmployeeName=e.Name,
- DepartmentName=d==null? "No Department " :d.Department_Name
- };
-
- Console.WriteLine("----------LEFT OUTER JOIN----------");
- foreach (var employee in result)
- {
- Console.WriteLine("Employee Name: " + employee.EmployeeName +"\t\t"+ "Department Name: " + employee.DepartmentName);
- }
-
- Console.ReadLine();
- }
- }
- }
- }
Output
Example of Cross Join With Extension Method
- using System;
- using System.Linq;
-
- namespace JoinOperations_Demo
- {
- class CrossJoin
- {
- static void Main()
- {
- using (DBModel db=new DBModel())
- {
- var result = db.Employees.Join(db.Departments, e=>true,d=>true,(e,d)=>new { e,d });
-
- foreach (var employee in result)
- {
- Console.WriteLine(" Employee Name: " + employee.e.Name + "\t\t\t" + "Department Name:" + employee.d.Department_Name);
- }
-
- Console.ReadLine();
- }
- }
- }
- }
Output
Example of Cross Join with SQL like syntax
- using System;
- using System.Linq;
-
- namespace JoinOperations_Demo
- {
- class CrossJoin
- {
- static void Main()
- {
- using (DBModel db=new DBModel())
- {
- var result=from e in db.Employees
- from d in db.Departments
- select new{ e, d };
-
- foreach (var employee in result)
- {
- Console.WriteLine(" Employee Name: " + employee.e.Name + "\t\t\t" + "Department Name:" + employee.d.Department_Name);
- }
-
- Console.ReadLine();
- }
- }
- }
- }
Output