Write JOIN Queries using LINQ to SQL and Entity Framework in C# and VB.Net

Last Reply 10 months ago By Mudassar

Posted 11 months ago

Hello Everybody,

I am having problems in joining tables using entity framework  6.1.3

help me by giving example with it is description and it syntax 

Suppose I want to Write the following queires using entityframework

INNER JOIN

SELECT Orders.OrderID, Customers.CustomerName
FROM Orders
INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID;

LEFT JOIN

SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID
ORDER BY Customers.CustomerName;

RIGHT JOIN

SELECT Orders.OrderID, Employees.LastName, Employees.FirstName
FROM Orders
RIGHT JOIN Employees ON Orders.EmployeeID = Employees.EmployeeID
ORDER BY Orders.OrderID;

FULL OUTER JOIN

SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
FULL OUTER JOIN Orders ON Customers.CustomerID=Orders.CustomerID
ORDER BY Customers.CustomerName;

SELF JOIN

SELECT A.CustomerName AS CustomerName1, B.CustomerName AS CustomerName2, A.City
FROM Customers A, Customers B
WHERE A.CustomerID <> B.CustomerID
AND A.City = B.City 
ORDER BY A.City;

thanks 

Posted 10 months ago

Simply write a SP with JOINS and call it using Entity Framework.

Entity Framework: Call Stored Procedure with Parameters using C# and VB.Net