Updated May 18, 2023
Introduction to LINQ Include
LINQ include helps out to include the related entities which loaded from the database. It allows retrieving the similar entities to be read from database in a same query. LINQ Include(), which points towards similar entities, must read from the database to get in a single query.
Syntax:
Let’s understand the following syntax,
var C_OrderDetails=context.customer details.Include ("OrderDetails").ToList();
In this syntax, we use the include method to combine the related entities in the same query, like merging multiple entities in a single query.
How does include work in LINQ?
LINQ Include(), which point towards the similar entities that must read from the database to get in a single query. Let’s understand the below example, like how the LINQ Include functionality loads the related entities automatically. To assume that the CustomerDetails Object has links to its OrderDetails and those orders have a reference of LineItems, those LineItems also had a reference to the ProductDetails; by using LINQ Include(), it allows you to specify the related entities to be read from the database in a single query.
Var COrderDetails=context.CustomerDetails.Include ("OrderDetails").ToList();
By using only SQL statements instead of using the Include() methods, we need to generate the following queries to retrieve the same above,
SELECT * FROM CustomerDetails;
Instead of using Include (“OrderDetails”), the code looks as follows,
SELECT * FROM CustomerDetails
JOIN OrderDetails
ON Customer_ID=OrderDetails.Customer_ID;
In the same single query, we can read all related entities from the database; in the case of using Include(), if we want to include the ProductDetails and LineItems also in the same query, it looks like as
Var Customer_OrderDetails=
context.CustomerDetails.Include("OrderDetails").Include("LineItems").Include ("ProducDetails").ToList();
We can make it use multiple calls to Include() to get the objects along various paths. If you require the objects in the same path, you must make a single call specifying the entire path.
Example
When using Include, if we have n number of queries to execute n number of times that is time-consuming, then it’s like select N+1 problem; this issue happens only because the lazy loading mechanism enables by default to execute a single query n number of queries to do something. So it’s better to avoid the select N+1 problem in Entity Framework; we need to use the Include Method, which helps to build a single query with required data using the Join clause, and this is the most resourceful way compared to executing queries multiple times.
Let’s see the sample program with the use of LINQ_Include. We need to include the namespace System.Data.The entity which the LINQ Include is an extension method of the Data.Entity namespace. Entity Framework version provides LINQ Include() by using EntityFramework.dll and System.Data.Entity.
Code:
using System;
using System.Data.Entity;
using System.Linq;
using System.Collections.Generic;
public class Program_LINQ_Include
{
public static void Main()
{
// to inserting the data
Inserting_Data();
using (var context = new EntityContext())
{
var customers = context.Customers
.Include(i => i.Invoices.Select(it => it.Items))
.ToList();
Displaying_Data(customers);
}
}
public static void Inserting_Data()
{
using (var context = new EntityContext())
{
context.BulkInsert(CustomerData());
context.BulkInsert(InvoiceData());
context.BulkInsert(ItemData());
}
}
public static List<Customer> CustomerData()
{
List<Customer> list = new List<Customer>()
{
new Customer() { Name ="Peter"},
new Customer() { Name ="Smith"},
new Customer() { Name ="James"}
};
return list;
}
public static List<Invoice> InvoiceData()
{
List<Invoice> list = new List<Invoice>()
{
new Invoice() { Date = new DateTime(2020,5,3), CustomerID = 1},
new Invoice() { Date = DateTime.Now.AddDays(-5), CustomerID = 1},
new Invoice() { Date = DateTime.Now.AddDays(-3), CustomerID = 1},
new Invoice() { Date = new DateTime(2020,4,15), CustomerID = 2},
new Invoice() { Date = new DateTime(2020,2,20), CustomerID = 3},
new Invoice() { Date = new DateTime(2020,5,22), CustomerID = 3},
};
return list;
}
public static List<Item> ItemData()
{
List<Item> list = new List<Item>()
{
new Item() { Name = "Mobile-Charger", InvoiceID = 1},
new Item() { Name = "Laptop-DELL", InvoiceID = 1},
new Item() { Name = "Stationeries", InvoiceID = 1},
new Item() { Name = "Note-Books", InvoiceID = 2},
new Item() { Name = "DataCard", InvoiceID = 2},
new Item() { Name = "PenDrive", InvoiceID = 3},
new Item() { Name = "Water-Bottles", InvoiceID = 3},
new Item() { Name = "Stationeries", InvoiceID = 3},
new Item() { Name = "DataCard", InvoiceID = 4},
new Item() { Name = "School-Bags", InvoiceID = 4},
new Item() { Name = "Table-Chairs", InvoiceID = 4},
new Item() { Name = "Lap-Table", InvoiceID = 4},
new Item() { Name = "Mobile-Charger", InvoiceID = 5},
new Item() { Name = "School-Bags", InvoiceID = 5},
new Item() { Name = "Stationeries", InvoiceID = 6},
new Item() { Name = "Laptop-DELL", InvoiceID = 6},
new Item() { Name = "Loptop-Cover", InvoiceID = 6},
new Item() { Name = "PenDrive", InvoiceID = 6},
new Item() { Name = "Memory-Card", InvoiceID = 6},
new Item() { Name = "Mobile-Charger", InvoiceID = 6},
new Item() { Name = "School-Bags", InvoiceID = 6},
new Item() { Name = "Touch-Pad", InvoiceID = 6},
};
return list;
}
public static void Displaying_Data(List<Customer> list)
{
foreach(var customer in list)
{
Console.WriteLine(customer.Name);
foreach(var invoice in customer.Invoices)
{
Console.WriteLine("\t" + invoice.Date);
foreach(var item in invoice.Items)
{
Console.WriteLine("\t\t" + item.Name);
}
}
}
Console.WriteLine("\t\t");
}
}
In the above program, the Entity Framework tells that the Customer Details require their Invoices too. This method in the dbcontext helps lazy loading secure the n+1 problem.
Output:
Let’s understand the above example, like how the LINQ Includes functionality to load the related entities. To assume that the Customer_Details Object has links to its Invoice_Details and those orders have an ItemData reference. Likewise, we can make several links to the related objects by using LINQ Include(), which allows you to specify the related entities to be read from the database in a single query.
Conclusion
I have explained the LINQ Include() method with several examples programmatically in this article. It enables us to retrieve the related entities from the database in the same query. Using the Include method in a single query, we can easily read all related entities from the database.
Recommended Articles
This is a guide to LINQ Include. Here we discuss the Introduction, Syntax, and working of include method, example, and code implementation. You may also have a look at the following articles to learn more –