Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / January 2008

Tip: Looking for answers? Try searching our database.

Linq lazy loading

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
fredd00 - 10 Jan 2008 14:21 GMT
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)

I bind a product list to a repeater that need to display
product.category.name (with should be lazy loaded)

-> when I use a linqdataobject as the repeater source , i get the
error that the context is closed
-> when I use a facade that returns a list of products - the
categories are not early nor lazy loaded
public static List<Product> GetProductList()
{
 using(context ctx = new context())
 {
  var list = from p in ctx.products
                 join c in ctx.categories on p.categoryid equals c.id
                 select p;
  return list.ToList<Product>();
 }
}

-> when I use

using (context ctx = new context())
{
  repeater.DataSource = from p in ctx.products
  repeater.DataBind();
}
this works since the context is open but i don't want to do it in my
web page and this is not using a LinqDataObject

I want to simply call a method that returns a list of product with
lazy loading of the category list.

how can i do this?

thanks
fredd00 - 10 Jan 2008 14:25 GMT
> Hi,
>
[quoted text clipped - 42 lines]
>
> thanks

seems like i found some good info

http://davidhayden.com/blog/dave/archive/2006/06/07/2983.aspx
and
http://davidhayden.com/blog/dave/archive/2007/08/03/LinqDataSourceHighPerformanc
eQueriesDataLoadOptionsAvoidingDatabaseRoundtrips.aspx

fredd00 - 10 Jan 2008 15:55 GMT
sorry i was too happy to find something those are about prefetching
and loadingOptions
good info but not exactly what i'm looking for

I want a list of products that if i ask product.category then it will
fetch the data, this after populating my list and releasing my context
object.
Nicholas Paldino [.NET/C# MVP] - 10 Jan 2008 16:26 GMT
Basically, you are going to have to do the loading yourself.  I actually
brought this up with the PM for LINQ-to-SQL at MS, indicating that I don't
want to have to do all of the loading myself, because as the type hierarchy
grows, so do the number of calls I need to place in other places to load the
hierarchy.

   I think that you could probably create a reflection-based routine
(assuming you don't have any strong base types which represent related
records) which will access the properties which expose other entities and
therefore, load them.

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

> sorry i was too happy to find something those are about prefetching
> and loadingOptions
[quoted text clipped - 3 lines]
> fetch the data, this after populating my list and releasing my context
> object.
fredd00 - 10 Jan 2008 18:48 GMT
On Jan 10, 11:28 am, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.com> wrote:
>     Basically, you are going to have to do the loading yourself.  I actually
> brought this up with the PM for LINQ-to-SQL at MS, indicating that I don't
[quoted text clipped - 18 lines]
> > fetch the data, this after populating my list and releasing my context
> > object.

Hi
thanks for the reply,

so the short answer is there is no real lazy loading if the context is
closed

i would need to implement it in the property
open a new context get the data a maintain it i my object

i don't see why microsoft did not do the extra step , it's not such a
big job but would have been a real implementation of lazy loading

thanks again
Nicholas Paldino [.NET/C# MVP] - 10 Jan 2008 20:32 GMT
BTW, it should be mentioned that what you want to do isn't really lazy
loading, it's really up-front loading.

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

> On Jan 10, 11:28 am, "Nicholas Paldino [.NET/C# MVP]"
> <m...@spam.guard.caspershouse.com> wrote:
[quoted text clipped - 38 lines]
>
> thanks again
fredd00 - 11 Jan 2008 14:48 GMT
On Jan 10, 3:32 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.com> wrote:
>     BTW, it should be mentioned that what you want to do isn't really lazy
> loading, it's really up-front loading.
[quoted text clipped - 49 lines]
>
> > thanks again

you are right in this exemple i should use up-front loading of the
child, which i am now doing

but for some other situation i might want to use lazy loading , and I
wanted to know if it was possible.
Seems like it's not possible unless I code it myself

thanks

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.