In this post, I will explain how to perform a self-join in LINQ and Entity Framework. I'm currently working on a project where I need to display employee details in a table, including their manager's name. The table structure consists of an `Employees` table with columns `Emp_Id` and `Mng_Id`, where `Mng_Id` represents the manager's ID.
To achieve this, I've decided to use a self-join on the `Employees` table. The goal is to retrieve the employee's name along with their manager's name.
CREATE TABLE [dbo].[Employee](
[Emp_Id] [int] NOT NULL,
[Name] [nvarchar](50) NULL,
[Mng_Id] [int] NULL,
CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED
(
[Emp_Id] ASC
)
) ON [PRIMARY]
select e.Name EmployeeName,m.Name Managername from Employee e
Left join Employee m on e.Mng_Id=m.Emp_Id
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
DemoDataBaseEntities db = new DemoDataBaseEntities();
var result = from e in db.Employees
join m in db.Employees on e.Mng_Id equals m.Emp_Id into temp
from res in temp.DefaultIfEmpty()
select new
{
Emploayee = e.Name,
Manager = res.Name
};
}
}
}
This LINQ query joins the Employees table with itself to retrieve information about employees and their respective managers, returning the results as a collection of anonymous objects with employee names and manager names.
Now i want to show “supermanager” if Mng_Id is null that means Employee doesn’t have any Mng_Id
select e.Name EmployeeName,Case when m.Name is NULL then 'SuperMager' else m.Name end as ManagerName from Employee e
Left join Employee m on e.Mng_Id=m.Emp_Id
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
DemoDataBaseEntities db = new DemoDataBaseEntities();
var result = from e in db.Employees
join m in db.Employees on e.Mng_Id equals m.Emp_Id into temp
from res in temp.DefaultIfEmpty()
select new
{
Emploayee = e.Name,
Manager = res.Name == null ? "SuperManager" : res.Name
};
}
}
}
This LINQ query joins the Employees table with itself to retrieve information about employees and their respective managers. If an employee does not have a manager, the string "SuperManager" is assigned as the manager's name.