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 point 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 are using include method to combine the related entities in same query like merging the multiple entities in a single query.
How does include works in LINQ?
LINQ Include() which point towards the similar entities must read from the database to get in a single query. Lets understand the below example like how the LINQ Include functionality to load the related entities automatically. To assume that the CustomerDetails Object has links to its OrderDetails and those orders has 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 like as follows,
SELECT * FROM CustomerDetails
JOIN OrderDetails
ON Customer_ID=OrderDetails.Customer_ID;
In a same single query we can read all related entities from the database, in 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 same path need to make a single call that specifies the entire path.
Example
When to use Include is, if we having n number of queries to execute n number of time 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 executing 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 make use of Include Method, it helps to build a single query with required data using Join clause and this is the most resourceful way as 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.Entity in which the LINQ Include is an extension method of the Data.Entity namespace. Entity Framework version gives the provision of 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 required their Invoices too. By using this method in the dbcontext it helps the lazy loading to secure the n+1 problem.
Output:
Let’s understand the above example like how the LINQ Include functionality to load the related entities. To assume that the Customer_Details Object has links to its Invoice_Details and those orders has a reference of ItemData. Likewise, we can make several links to the related objects by using LINQ Include() it allows you to specify the related entities to be read from the database in a single query.
Conclusion
In this article, I have explained the LINQ Include() method with several example programmatically. It allows retrieving the related entities to be read from database in same query. By using the Include method we can easily read all related entities from the database in a single query.
Recommended Articles
This is a guide to LINQ Include. Here we discuss the Introduction, Syntax and working of include method along with example and code implementation. You may also have a look at the following articles to learn more –
5 Online Courses | 19 Hands-on Project | 90+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses