Question

I'm using LINQ with an ODATA web service

from tp in TyrePatterns
from t in tp.Tyres
where t.Diameter == 195
select tp

Seems simple enough, right? Tyres is a propery of TyrePatterns. So just to be sure you can see what I want to do, what I'm doing in the magical world of SQL would look something like:

SELECT DISTINCT TyrePatterns.Name
FROM TyrePatterns
INNER JOIN Tyres ON Tyres.TyreID = TyrePatterns.TyreID
WHERE Tyres.Diameter = 195

On our site, and in LINQPad, the LINQ code gives the run-time error:

System.NotSupportedException: Can only project the last entity type in the query being translated.

Insight into what this error actually means and a solution would be awesome.

Was it helpful?

Solution

In OData joining the two tables can be done in two ways: Navigations, something like this: /TyrePatterns(id)/Tyres?$filter=Diameter eq 195 But this assumes you know the id of the TypePattern you're looking for (Which doesn't seem to be what you're looking for)

Or expansions: /TyrePatterns?$expand=Tyres But then you can only apply filters to the TyrePatterns, not to the exapnded Tyres.

So the right way to go about this query is the other way round: /Tyres?$filter=Diameter eq 195&$expand=TyrePattern This will return all tires with Diameter 195 and it will also include their TypePattern (assuming the navigation property is bi-directional. It's not exactly the one you wanted, but it's the closest you can get.

Translated to LINQ this would look something like

from t in Tyres.Expand("TyrePatterns") where t.Diameter == 195 select t

You can then select just the TyrePatterns on the client easily.

The limitation is that you can only apply filter to the last segment in the navigation (the path part of the URL).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top