In this article, I will explain self join using Entity framework step by step.
I have table Employees with column Emp_Id and Mng_Id. Now I want to perform self join on this using the Entity Framework.I need to display the employee name and it’s Manager details below Image.
Let start by adding a console application and Add the ADO .NET Entity Data Model.
Sql Table Script:-
CREATE TABLE [dbo].[Employee](
[Emp_Id] [int] NOT NULL,
[Name] [nvarchar](50) NULL,
[Mng_Id] [int] NULL,
[Contact] [nvarchar](50) NULL,
[Address] [nvarchar](150) NULL,
CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED
(
[Emp_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]
Self Join Using Left Join with Sql Query :-
select e.Name EmployeeName,m.Name Managername , m.contact MangerContact,m.Address MangerAddress
from Employee e
Left join Employee m on e.Mng_Id=m.Emp_Id
Self Joint Using Left Join with Entity Query :-
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,
MangerContact=res.Contact,
MangerAddress=res.Address
};
}
}
}
Result:
Self Join Using Inner Join with Sql Query :-
SELECT c2.*
FROM Employee c1 INNER JOIN Employee c2
ON c1.Emp_Id = c2.Mng_Id
Self Joint Using Inner Join with Entity Query :-
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 c1 in db.Employees
join c2 in db.Employees on c1.Emp_Id equals c2.Mng_Id
select c2;
}
}
}
Result:
The post Undestand-How to Perform Self join in Entity framework appeared first on Software Development | Programming Tutorials.
Read More Articles
- Write a value which contain comma to a CSV file in c#?
- Reading CSV File with cells containing commas c#
- Split CSV with columns may contain ‘,’ Comma C#
- [Simple Way]-Cascading DropDownList in Asp.Net Mvc Using Jquery Ajax
- [Simple Way]-How to get data from database using JQuery Ajax in asp net MVC
- [Simple Way]-ASP.NET Core Upload Multiple File Web API model
- [Simple Way]- Image Upload in .NET Core Web API
- [Easy Way]-Receive File and other form data together in ASP.NET Core Web API
- Replace image in word document using C#
- How to add new rows to an existing word document table in C#
- How to perform rollback in Entity Framework 5?
- How do I make Entity Framework not join tables?
- Table Join performance issue with Entity Framework
- How to upgrade DataAnnotations for Entity Framework 5 Code First
- ASP.NET C# Entity Framework - How to update Foreign Key properly?
- How to use Entity Framework with Stored procedures and POCOs
- How can I execute an Oracle function from within a LINQ expression in Entity Framework 4?
- How to group columns by table name in LINQ Method Syntax in Table Relationships C# ASP .NET Entity Framework Core 6 Web API?
- How is the relationship between the table name and entity framework 6 made?
- How to understand "?." symbol in Entity Framework source code?
- How to Improve Entity Framework bulk insert
- How to edit the sql query that Entity framework generated?
- How to add a filestream column in the Entity Framework 4.0 model first designer?
- How to check if a delete operation succeeds in ASP.NET MVC using Entity Framework
- How do I create an in memory DbContext with Entity Framework 6?
- How can I get the value of an entity from LuisResult object? (LUIS with Bot Framework .NET)
- How to remove references to Entity Framework in other layers
- How Entity Framework works
- How does Entity Framework handle Default constraints in the database table?
- Using Entity Framework 4.1, how do I configure one to many relationship with composite keys using different field names?
- How to know how many connections are currently open by asp.net MVC3 app using Entity Framework 4.1
- How do I debug/profile for performance when using Entity Framework 4.1
- How to start with entity framework and service oriented architecture?
- Request Entity Too Large: How to Fix this Error with Microsoft Bot Framework on Teams
- How do I properly seed data with a circular dependency in Entity Framework Core?
- How Entity Framework 6 decides if the FK should be defined with ON DELETE CASCADE
- How to Define Foreign Key Relationships in Entity Framework other than the default method
- How can I implement Entity Framework Repository Design Pattern in my C# DAL library?
- How well does Entity Framework 6 support .NET 4.0?
- How to set foreign key where column names are different using Entity Framework 5 Fluent API?
- Remove any records with the same IDs (not removing only the duplicates)
- C# Cades P7M with Smartcard
- Find the rectangle of the currently selected item in a list box
- Interfacing with a localhost Windows service
- Using Redemption RDOContactItem cannot save more than a number of contacts MAPI_E_TOO_BIG
- How can I ignore the property name that I use in the jsonproperty attribute, provided that it is only once
- Custom CheckListBox Honor 2 columns
- Query String Encryption problem while postback events in asp.net 3.5
- Log out of Documentum Server
- What is the behavior of Authorize attribute with different authentication scheme in action and controller
- FormData is empty in parent when passed from child component
- MonoTouch MKLocalSearch Example
- how to add new type to an existing class or object in asp.net mvc
- How to use extension-method that uses interface
- Is there an easy solution to my collision issue?
- Regular Expression Validation - Range Validation with minium length value and undefinited maximum length
- Fishing mini game automatically restarting (Unity, C#)
- WebAPI - routes
- Setting variable through reflection
- Enable/disable button based on text input in Blazor Server Side