O'Reilly Forums: Doubt On Interpreting Navigation To Entity Reference - O'Reilly Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Doubt On Interpreting Navigation To Entity Reference

#1 User is offline   caof2005 

  • New Member
  • Pip
  • Group: Members
  • Posts: 6
  • Joined: 26-December 11

Posted 02 January 2012 - 09:43 AM

Hi folks with a newbe doubt that I will thank, you could clarify me.
This is the context,

I'm in chapter 4 of "Programming EF 2nd ed". on section "Navigating to an EntityReference."
Typing, compiling and trying to understand the code:

var addresses = from a in context.Addresses
where a.CountryRegion == "UK"
select new { a,
a.Contact.FirstName,
a.Contact.LastName };

foreach (var address in addresses)
{
Console.WriteLine("{0} {1} {2} {3}", address.FirstName, address.LastName, address.a.Street1, address.a.City);
}


Although it perfectly compiles and run, the thing that I still don´t get is why it is possible that the compiler can allow me to get directly a.FirstName and a.LastName properties?, Overall taking into account that when we got the "addresses" variable via the query, the "structure" that gets defined is set of: a (an address) + a.Contact.FirstName (aka, the FirstName property attached or linked to the Contact navigation property of the a address) + a.Contact.LastName (aka, the LastName property attached or linked to the Contact navigation property of the a address).
I mean, in my understanding if I want to get the FirstName or LastName properties I should use Contact navigation property in the foreach cycle:

foreach (var address in addresses)
{
Console.WriteLine("{0} {1} {2} {3}", address.Contact.FirstName, address.Contact.LastName, address.a.Street1, address.a.City);
}

Does the supposition/assumptions I´m doing are correct?

Any clarification will be very helpfull.
Best Regards
0

#2 User is offline   Flyware 

  • New Member
  • Pip
  • Group: Members
  • Posts: 6
  • Joined: 03-January 12

Posted 06 January 2012 - 10:33 PM

From one newbie to another, I hope this helps.

Perhaps what is confusing you is that the return variable for you query is called addresses. The query is returning an Address entity and two scalar properties of the related Contact for the Address.

If you break on the start of the for loop and then hover over the variable name, you can see that it is made up of an Address (called a), plus Firstname and Lastname as in the attached pic.

Attached Image: EF.jpg

Therefore you can reference First/Last Name directly, but need a.Street1 and a.City in the WriteLine().

If your query just selected and Address entity, then you would need the .Contact in front of FirstName and LastName and would not need the .a for Street1 and City.

Sorry if this isn't too clear but that's how I understand it.

Martin
0

#3 User is offline   JulieLerman 

  • Advanced Member
  • PipPipPipPipPipPipPipPip
  • Group: O'Reilly Author
  • Posts: 301
  • Joined: 17-September 08

Posted 12 February 2012 - 05:18 PM

Thanks Flyware for helping to provide support in the forum. I am truly grateful!

@caof,
by the time we are iterating through the results we are no longer working with instances of an Address type.

Let me rename some variables to see if that helps.

var listOfAnyonmousTypes= from a in context.Addresses
where a.CountryRegion == "UK"
select new { Address=a,
ContactFirstName=a.Contact.FirstName,
ContactLastName=a.Contact.LastName };

foreach (var anonymousType in listOfAnyonmousTypes)
{
Console.WriteLine("{0} {1} {2} {3}",
anonymousType .ContactFirstName,
anonymousType .ContactLastName,
anonymousType .Address.Street1,
anonymousType .Address.City);
}

In the query, I am creating a new type on the fly with three properties. The first is an address sub type, the second is a string and the third is a string.

Then when I iterate through the results, I print out both of the string properties and from the address property, the street & city.

Does that make sense?

There is a section somewhere in there explaining anonymous types.

Julie
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users