Entity Framework is a popular ORM framework in which developers don't have to write SQL queries and focus more on writing business logic in the C# end. Instead, to read or write data in a database we use LINQ Query or Lambda Expression in our data layer of the project.
LINQ queries are very similar to SQL queries and are converted to SQL queries by .Net Runtime when we run our application.
LINQ queries generally have three parts like below
from ...
where ...
select ...
Using the JOIN operator is very common in SQL Queries as they help us to get data from two or more tables in a database. In this article, we will see how to join two or more tables in LINQ Query to get the final data.
In this example, we are using Sales.Customer and Sales.SalesOrderHeader table in AdventureWorks2014 database.
INNER JOIN in LINQ Query
/// <summary>
/// Gets all the customer which has corresponding Order in SalesOrderHeader table
/// </summary>
/// <returns></returns>
public IEnumerable<CustomerOrder> GetAllCustomerWithOrder()
{
var customerOrders =
from customer in _dbCoreContext.Customers
join salesorderheader in _dbCoreContext.SalesOrderHeaders
on customer.CustomerId equals salesorderheader.CustomerId
where salesorderheader.TotalDue > 10000
select new CustomerOrder
{
CustomerId = customer.CustomerId,
AccountNumber = customer.AccountNumber,
OrderDate = salesorderheader.OrderDate,
SalesOrderNumber = salesorderheader.SalesOrderNumber,
TotalDue = salesorderheader.TotalDue
};
var result = customerOrders.ToList().AsEnumerable();
return result;
}
LEFT JOIN in LINQ Query
/// <summary>
/// Get all the customers with or without Order
/// </summary>
/// <returns></returns>
public IEnumerable<CustomerOrder> GetAllCustomer()
{
var customerOrders =
from customer in _dbCoreContext.Customers
join salesorderheader in _dbCoreContext.SalesOrderHeaders
on customer.CustomerId equals salesorderheader.CustomerId into nc
from subSalesOrderHeader in nc.DefaultIfEmpty()
where subSalesOrderHeader.TotalDue > 10000
select new CustomerOrder
{
CustomerId = customer.CustomerId,
AccountNumber = customer.AccountNumber,
OrderDate = subSalesOrderHeader.OrderDate,
SalesOrderNumber = subSalesOrderHeader.SalesOrderNumber,
TotalDue = subSalesOrderHeader.TotalDue
};
var result = customerOrders.ToList().AsEnumerable();
return result;
}
RIGHT JOIN in LINQ
/// <summary>
/// Get all the orders with or without Customer associated with it.
/// </summary>
/// <returns></returns>
public IEnumerable<CustomerOrder> GetAllOrderWithCustomer()
{
var customerOrders =
from salesorderheader in _dbCoreContext.SalesOrderHeaders
join customer in _dbCoreContext.Customers
on salesorderheader.CustomerId equals customer.CustomerId into nc
from subCustomer in nc.DefaultIfEmpty()
where salesorderheader.TotalDue > 10000
select new CustomerOrder
{
CustomerId = subCustomer.CustomerId,
AccountNumber = subCustomer.AccountNumber,
OrderDate = salesorderheader.OrderDate,
SalesOrderNumber = salesorderheader.SalesOrderNumber,
TotalDue = salesorderheader.TotalDue
};
var result = customerOrders.ToList().AsEnumerable();
return result;
}
Hope you have understood how to use JOIN in LINQ Query.
Share This Post
Support Me