Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize delayed loading in Linq

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Xiaobian to share with you how Linq to achieve delayed loading, I believe most people still do not know how to share this article for your reference, I hope you have a lot of gains after reading this article, let's go to understand it together!

Linq delayed loading

When querying an object, you are actually querying only that object. This object is not automatically fetched at the same time. This is Linq delayed loading.

For example, you might want to view customer data and order data. You do not initially need to retrieve all order data pertaining to each customer. The advantage is that you can use Linq delayed loading to delay retrieval of additional information until you actually need to retrieve it. Please see the following example: retrieve CustomerID and query OrderID according to this ID.

var customs = from c in db.Customers where c.City == "Sao Paulo" select c; //The query syntax above does not cause the statement to execute immediately, it is merely a descriptive statement that is executed only when needed. foreach (var ord in dust.Orders) { //View customer data and order data simultaneously} }

Statement Description: The original query did not request data, and navigating through links to retrieved objects can lead to triggering new queries to the database.

Linq Delayed Loading: LoadWith Method

If you want to query a collection of objects at the same time. LINQ to SQL provides DataLoadOptions for loading objects immediately. Methods include:

The LoadWith method is used to immediately load data related to the primary target.

AssociateWith method, used to filter objects retrieved for a particular relationship.

Use the LoadWith method to specify which data related to the primary target should be retrieved simultaneously. For example, if you know you need information about a customer's order, you can use LoadWith to ensure that order information is retrieved at the same time as customer information. This method allows you to access the database only once, but obtain two sets of information at the same time.

In the following example, we instruct DataContext to load Orders along with Customers by setting DataLoadOptions, and retrieve all Orders of all Customers located in Sao Paulo when executing the query. This way, successive accesses to the Orders property of the Customer object do not trigger new database queries. SQL statements generated at execution time use left joins.

NorthwindDataContext db = new NorthwindDataContext(); DataLoadOptions ds = new DataLoadOptions(); ds.LoadWith(p => p.Orders); db.LoadOptions = ds; var custs = ( from c in db2.Customers where c.City == "Sao Paulo" select c); foreach (var cust in custs) { foreach (var ord in cust.Orders) { Console.WriteLine("CustomerID {0} has an OrderID {1}. ", cust.CustomerID, ord.OrderID); } }

Statement Description: Use LoadWith to request related data during the original query so that no additional round trips to the database are required later when navigating through the retrieved objects.

Linq Delayed Loading: AssociateWith Method

Specify a subquery using the AssociateWith method to limit the amount of data retrieved.

In the following example, the AssociateWith method limits the Orders retrieved to those that have not been shipped that day. Without this method, all Orders are retrieved, even if only a subset is needed. But generating SQL statements will reveal that a lot of SQL statements are generated.

NorthwindDataContext db2 = new NorthwindDataContext(); DataLoadOptions ds = new DataLoadOptions(); ds.AssociateWith ( p => p.Orders.Where(o => o.ShipVia > 1)); db2.LoadOptions = ds; var custs = from c in db2.Customers where c.City == "London" select c; foreach (var dust in custs) { foreach (var ord in dust.Orders) { foreach (var orderDetail in ord.OrderDetails) { //available for query dust.CustomerID, ord.OrderID, ord.ShipVia, //orderDetail.ProductID, orderDetail.Product.ProductName } }

Statement Description: The original query did not request data, and navigating through links to retrieved objects ends up triggering a new query to the database. This example also illustrates how you can use Assert With to filter relational objects when Linq latencies them.

The above is "Linq how to achieve delayed loading" all the content of this article, thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report