Colin Desmond
Colin Desmond

Reputation: 4854

oData and EF4 - different result for the same query

I am running the following query...

(from txRx in TxRxes
where txRx.Serial == "someSerial"
select txRx).Single().TxRxModes.Count()

When I run this on the database using EF4 in LinqPad, I get the correct result of 1 back. When I run exactly the same query on the same database through oData (using the same underlying context) I get 0.

Why?

Upvotes: 0

Views: 223

Answers (1)

Vitek Karas MSFT
Vitek Karas MSFT

Reputation: 13320

The Single forces the query before it to execute in OData (technically it's WCF Data Services). So the query sent to the server is just the selection of a txRx with the specified serial. The difference from EF is, that EF will lazy load navigation properties (in your case the TxRxModes) and thus when you access it to count it, it works. WCF Data Services doesn't perform lazy load, since it could be very expensive (HTTP request to a remote server), and thus the TxRxModes is just an empty collection. To work around this, you should be able to modify your code to preload the navigation property in question (so eager loading):

(from txRx in TxRxes.Expand("TxRxModes")
where txRx.Serial == "someSerial" 
select txRx).Single().TxRxModes.Count()

Note the additional Expand call which causes the query to pull not just the txRx but also all its related TxRxModes from the server (in one query).

Upvotes: 1

Related Questions