Trending October 2023 # How Include Works In Linq With Examples? # Suggested November 2023 # Top 11 Popular | Khongconthamnam.com

Trending October 2023 # How Include Works In Linq With Examples? # Suggested November 2023 # Top 11 Popular

You are reading the article How Include Works In Linq With Examples? updated in October 2023 on the website Khongconthamnam.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested November 2023 How Include Works In Linq With Examples?

Introduction to LINQ Include

Web development, programming languages, Software testing & others

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? 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; 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 chúng tôi entity which the LINQ Include is an extension method of the Data.Entity namespace. Entity Framework version provides LINQ Include() by using chúng tôi 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() { Inserting_Data(); using (var context = new EntityContext()) { var customers = context.Customers .ToList(); Displaying_Data(customers); } } public static void Inserting_Data() { using (var context = new EntityContext()) { context.BulkInsert(CustomerData()); context.BulkInsert(InvoiceData()); context.BulkInsert(ItemData()); } } { { new Customer() { Name ="Peter"}, new Customer() { Name ="Smith"}, new Customer() { Name ="James"} }; return list; } { { new Invoice() { Date = new DateTime(2023,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(2023,4,15), CustomerID = 2}, new Invoice() { Date = new DateTime(2023,2,20), CustomerID = 3}, new Invoice() { Date = new DateTime(2023,5,22), CustomerID = 3}, }; return list; } { { 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; } { 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("tt" + item.Name); } } } Console.WriteLine("tt"); } }

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 –

You're reading How Include Works In Linq With Examples?

Update the detailed information about How Include Works In Linq With Examples? on the Khongconthamnam.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!