views:

258

answers:

1

I have a report that tries to show Order.Customer.Address.Line1 in a report. My Order object comes from a Linq2Sql DBML file.

In the report i define it as Fields!Customer.Value.Address.Line1, but it comes out as "#Error".

I then tried "touching" the variable before passing it to the RDLC, and lo-and-behold it suddenly worked.

Apparently this only happens in second level, as I can print Fields!Customer.Value.Name without problems.

I could then just touch any and all second-level-objects, but it seems .. foolish.

Does anyone know a way to make it work, so that I won't need to do that?

A: 

WAG... but are you disposing of the DataContext prior to your report being viewed? Linq to Sql uses deferred execution, so your "second level objects" aren't there until you access them. Linq to Sql entity classes store information on how to access some things (like collections of related records) rather than load up everything at once. If the DataContext used to load your entities goes bye-bye before you access these delay-loaded parts, its going to fail. By "touching" them first, they are loaded before the DataContext goes away.

One thing you can do is to turn off lazy loading on those parts of your models you anticipate using in this scenario. Here's a blog post about doing this. The other option is to "touch" these prior to returning them, by calling "ToArray()" or "ToList()" or something similar to these delay loaded collections. What's best depends on your design. Turning off lazy loading is probably the quickest and simplest solution.

Will
Thanks for the link. I'm creating the report, and rendering it to a PDF in one go, so I don't *think* the DataContext goes away. I also tried setting "ExecuteInCurrentAppDomain", but i'm not really sure what that was supposed to achieve. It was just something I half-read somewhere.
Soraz