I am wondering if there are any alternatives to using the Expand key word when performing an LINQ to ADO.net Data Services query. The expand method does get me the data I am interested in, but it requires me to know all of the sub-objects that I am going to be working with in advance. My absolute preference would be that those sub-objects would be lazy loaded for me when I access them, but this doesn't look to be an option (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 anyone have any suggestions/best practices/alternatives for this situation? Thanks.
===== Example Code using Member that has a MailingAddress =====
Works:
var me = (from m in ctx.Member.Expand("MailingAddress")
where m.MemberID == 10000
select m).First();
MessageBox.Show(me.MailingAddress.Street);
Would Prefer (would really like if this then went and loaded the MailingAddress)
var me = (from m in ctx.Member
where m.MemberID == 10000
select m).First();
MessageBox.Show(me.MailingAddress.Street);
Or at least (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);
Loading sub-objects via ADO.net Data Services seem to have two choices:
Accomplished by .Expand("[MemberVariableName]") on the LINQ to Data Services example
var me = (from m in ctx.Member.Expand("MailingAddress")
where m.MemberID == 10000
select m).First();
MessageBox.Show(me.MailingAddress.Street);
Accomplished by calling .LoadProperty on the context and passing it the variable and the property that should be lazy loaded.
var me = (from m in ctx.Member
where m.MemberID == 10000
select m).First();
ctx.LoadProperty(myMember, "MailingAddresses");
MessageBox.Show(me.MailingAddress.Street);
With LINQ to Entities you can also use the Include method. You can apply this to me after it's declared but before it's executed, e.g.:
me = me.Include("MailingAddress");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With