jjbuchholz (1) [Avatar] Offline
#1
With the standard northwind.dbml, "backward referencing" works fine

Dim customers = _
From order _
In northwind.Orders _
Where order.ShipCity = "London" _
Select order.Customers.Phone


but "forward referencing"

Dim customers = _
From customer _
In northwind.Customers _
Where customer.Orders.ShipCity = "London" _
Select customer.Phone


leads to the error

"ShipCity" ist kein Member von "System.Data.Linq.EntitySet(Of Orders)".

Bug or feature?
jwooley (123) [Avatar] Offline
#2
Re: Up and down the key chain
Orders is the collection of order items rather than the actual item, thus you can't access the ShipCity on the Orders collection. When you are going the other way, Order has a single Customer object that it references, thus you are able to drill from Order directly to the single Customer it is related to. This is why your first query works. There are a couple solutions to your second query. First, you can use the Any method to find records where any of the orders were shipped to London:

Dim query = From c In dc.Customers _
Where c.Orders.Any(Function(o) o.ShipCity = "London") _
Select c

Alternatively, you could reference the Orders separately in a SelectMany clause. In this case, you would need to limit the results with a Distinct clause:

Dim q2 = From c In dc.Customers _
From o In c.Orders _
Where o.ShipCity = "London" _
Select c Distinct

The key take-away is to watch which way the one and many relationships are working in the object heirarchies.

Jim