Answered by:
How To Access Entity Child Object Values

Question
-
Hi,
I am having some trouble retrieving the value of an enitity child object. I have a parent/child relationship as such:
Here is the linq statement that gets the addresses for a particular contact:
Public Function GetAddresses(ByVal contactId As Integer) As List(Of Addresses)
Using TalentContext = New TalentManagerDBEntities
Dim Addr = From a In TalentContext.Addresses _
From ca In TalentContext.ContactAddresses _
Where a.AddressId = ca.Addresses.AddressId And ca.Contacts.ContactId = contactId _
Select a
'Return TalentContext.Addresses.ToList
Return Addr.ToList
End Using
End Function Public Function GetCountries() As List(Of ConfigCountries)
Using TalentContext = New TalentManagerDBEntities
Return TalentContext.ConfigCountries.ToList
End Using
End FunctionIn the code behind of my windows form, i have the following code to set the value of the stateId and countryId for each address:
Dim addresses() As Addresses = wcf.GetAddresses(1)
Dim countryid as Integer = addresses(i).ConfigCountries.CountryId
The error occurs on the last code, "Object reference not set to an instance of an object".
My question is how do get he CountryId of address(i)? When I expand the results in debug mode i'm able to see the values in the result of address(1).ConfigCountries.
Thanks.
Thursday, February 23, 2012 5:05 AM
Answers
-
On 2/23/2012 9:31 AM, askarii wrote:> I am looping and i is 0 (zero) at this time. Thanks.You do know that you would have to use an "Include" statement on thequery with the parent/child relationship to get the children objects fora parent.Or you would have had to do a Load of the child of a parent as youlooped over the parent collection to address the child object.
- Marked as answer by Allen_MSDN Monday, February 27, 2012 2:08 AM
Thursday, February 23, 2012 2:43 PM
All replies
-
what is i? are you looping, try 0(zero) if you are only expecting one record.
if not construct a loop.- Edited by buthfa Thursday, February 23, 2012 10:38 AM
Thursday, February 23, 2012 10:36 AM -
I am looping and i is 0 (zero) at this time. Thanks.Thursday, February 23, 2012 2:31 PM
-
On 2/23/2012 9:31 AM, askarii wrote:> I am looping and i is 0 (zero) at this time. Thanks.You do know that you would have to use an "Include" statement on thequery with the parent/child relationship to get the children objects fora parent.Or you would have had to do a Load of the child of a parent as youlooped over the parent collection to address the child object.
- Marked as answer by Allen_MSDN Monday, February 27, 2012 2:08 AM
Thursday, February 23, 2012 2:43 PM