What are some alternatives to expanding in a LINQ to ADO.net Data Service query?

entity-framework lazy-loading linq wcf-data-services

Question

I'm wondering whether there are any LINQ to ADO.net Data Services queries that don't need the Expand key word. The expand technique does indeed provide me with the information I am looking for, but it requires that I have a complete understanding of all the sub-objects I want to use in advance. However, it doesn't seem that this is an option. My ideal preference would be for those sub-objects to be lazily loaded for me when I access them (I could add this lazy loading to the get on that sub-object property, but it gets wiped out when I do an update of the data service reference).

Does anybody have any recommendations for this problem, including best practices or alternatives? Thanks.

Example of a Code Using a Member with a MailingAddress =====

Works:

var me = (from m in ctx.Member.Expand("MailingAddress")
          where m.MemberID == 10000
          select m).First();
MessageBox.Show(me.MailingAddress.Street);

I'd rather (I would really like it if this loaded the MailingAddress after that)

var me = (from m in ctx.Member
          where m.MemberID == 10000
          select m).First();
MessageBox.Show(me.MailingAddress.Street);

In any case (note: something similar to this, with MailingAddressReference, works on the server side if I do so as LINQ to Entities in a Service Operation)

var me = (from m in ctx.Member
          where m.MemberID == 10000
          select m).First();
if (!(me.MailingAddress.IsLoaded())) me.MailingAddress.Load()
MessageBox.Show(me.MailingAddress.Street);
1
5
10/7/2008 7:45:28 AM

Accepted Answer

using ADO.net to load child objects It seems that Data Services has two options:

Aware of loading

.Expand("[MemberVariableName]") in the LINQ to Data Services sample does this.

var me = (from m in ctx.Member.Expand("MailingAddress")          
         where m.MemberID == 10000          
         select m).First();
MessageBox.Show(me.MailingAddress.Street);

Slow loading

Calling accomplished it. Passing the variable and the property that should be lazily loaded to loadproperty on the context.

var me = (from m in ctx.Member          
          where m.MemberID == 10000          
          select m).First();
ctx.LoadProperty(myMember, "MailingAddresses");
MessageBox.Show(me.MailingAddress.Street);
9
10/10/2008 3:27:33 AM

Popular Answer

Additionally, you may utilize the Include technique with LINQ to Entities. I may use this after it is defined but before it is performed, for example:

me = me.Include("MailingAddress");


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow