473,811 Members | 3,299 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Linq question: loading an object graph

Let's say we have your basic Invoices and InvoiceItems table. If we load
this in with LINQ:

var query = from i in db.Invoices
select i;

When I then loop through my invoices, if I wanna access the lineItems for a
particular invoice, LINQ runs another query behind the scenes and goes and
gets them for me. So if I wanted to loop through my invoices and print out
the number of invoices thusly:

foreach(Invoice i in query)
WriteLine(i.som eInfo, i.lineItems.cou nt);

This means I have to go to the db each time through the loop. If I have
1000 invoices, I make that many round trips to the db, plus the original
query to get the invoices in the first place.

Similarly, if I used LINQ to just gimme the lineitems and then I wanted to
loop through them and access lineItem.Invoic e, that means I have to go to
the db each time through the loop. In other words, any "aggregate" objects
don't get filled by the original LINQ query; LINQ waits until you access
them and goes and gets them in a "just in time" fashion.

Now, this is nice and convenient. It takes hardly any code to write this
stuff, and it gets what it needs when it needs to and you don't have to
concern yourself with it. But, I often work with graphs such as this that
have maybe 1000 parent objects, and each one might have 100 children (and
sometimes those children have children). If I have to loop through one of
my object graphs, it would be TONS of trips to the db and long wait times
for my users.

vIs there any way to tell LINQ to get more stuff initially?
Mar 28 '08 #1
1 2235

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
431
by: Rein Petersen | last post by:
Hi all, I'm toying with reflection to try and build a recursive function that generates treenodes of a treeview that represent any given object's graph (of child objects): public TreeNode BuildNode(ref object o) { TreeNode tn; treenode.Tag = o;
0
1483
by: Rein Petersen | last post by:
Hi all, I'm toying with reflection to try and build a recursive function that generates treenodes of a treeview that represent any given object's graph (of child objects): public TreeNode BuildNode(ref object o) { TreeNode tn; treenode.Tag = o;
5
5058
by: wbekker | last post by:
Hi, I'm searching for a good pattern for the following problem: In a large object tree, all object implement a property called IsDirty. That flag is set when a property is modified. If a child object is dirty, the parent object must return IsDirty = true as well. This works. I now want to have an event on the root object of the tree, that signals when one object in the tree is set to dirty. What is a good approach for
3
1140
by: STom | last post by:
Ok, if I have a class: Public Class MyXClass Public Property X as Integer ..... End Class In my code if I have two instances of this class: Dim myX1 as new MyXClass
1
1674
by: Lucile | last post by:
Hi to All, I am new to this newsgroup, so please bare with me. Here is the deal: I have the following Object Graph: Icell (Interface) Iline (Interface) Itable (Interface) Classes: Cell: implements Icell Line: inherits Arraylist Implements Iline
0
1086
by: Creativ | last post by:
After reading the article (Jeffrey's) about GC, I wonder how to build a object graph to see what object is still alive and if so which object is referring to them. This is also important in practice. Since in a application where large scale computation is going on, it's important to know which object are kept alive incidently. I've seen CLR profiler. But I wonder if I can build something myself simular.
4
549
by: BeSharp | last post by:
I recently stumbled across a pretty interesting LINQ to SQL question and wonder, whether anybody might have an answer. (I'm doing quite some increasing LINQ evangelism down here in Germany.). Assume I want to select rows from a database and check whether a specific column contains keywords from a list of keywords. The following works just fine: List<stringsearchTerms = new List<string>() { "Maria", "Pedro" };
2
5894
by: fredd00 | last post by:
Hi, i'm trying to use lazy loading with Linq to sql and related objects seems like you can only call the child object if the context is still open, this is not real lazy loading. here is my actual implementation my class product has a categories collection (using a one-to-many relation in .dbml)
4
220
by: CSharper | last post by:
I would like know, will it be possible to run a linq statement generated on the fly by a program? Lets say, I have string like the following string t = "from c in doc.Decentents('Value') select c"; Is there anyway I would able to run this? Thanks.
0
9734
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10652
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10395
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10408
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10137
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7673
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5700
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4346
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3874
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.