Vaccano
Vaccano

Reputation: 82467

Linq OData "Where" clause on nested list

Say I have the following query OData Linq Query (run against http://odata.netflix.com/v2/Catalog):

Genres.Where(x=>x.Name=="Adventures")
      .Select(y=> new
                  {
                    Genre = y.Name, 
                    Movie = y.Titles.Select(l=> new 
                                                {
                                                   l.Name, 
                                                   l.AverageRating
                                                 })
                    })

How could I change this query to give me the rows where AverageRating was 3?

(NOTE: The point of this question is to find out how to do a where clause on properties of an expanded sub list of my main level query item. My real query is not even against the Netflix OData feed.)

Upvotes: 3

Views: 2266

Answers (1)

Vitek Karas MSFT
Vitek Karas MSFT

Reputation: 13320

The short answer is that it's not possible currently. The $filter always (and only) applies to the top-level entity set. There's currently no way to filter expanded entity sets. The $filter can reach inside the expanded entity sets but the result will always filter the top-level set. In V2 it works for singleton navigation properties (the expression in $filter can traverse those), in V3 you can use the any/all to also incorporate collection navigation properties.

The reason this can't work is that the OData protocol doesn't define the URI syntax for nested filters. In fact it doesn't define almost any operator on the expanded entity sets except for the expansion itself and projections.

Upvotes: 1

Related Questions