Can an OData query specify a filter that references a nested object?

Here is the setup: I have a “Student” that has an associated entity “Course” (from 1 to many). Each "course" has an associated entity "Period", which contains all the details of the time and date of the course. I want to return all students who completed the course in 2011.

I tried this:
~ / Student ()? $ Expand = Course / Period & $ filter = Course / Period / Year eq 2011
but this leads to an error: no property 'Period' exists in type 'System.Data.Objects.DataClasses.EntityCollection`1 [[Course]]

It is clear that Period is being treated as a property, not an Entity, but I'm confused because the following query returns the expected results and uses almost the same syntax:
? ~ / Student () $ = expand course / Period & $ = choose course / Period / year

So am I doing something wrong with the $ filter syntax, or is this not possible?

TIA for any understanding.

+4
source share
2 answers

The filter will work if the navigation property is singleton, but since it is a collection (from 1 to many), the filter will not work. Mostly because it is not clear what this will mean. Do you want students who have all their courses in 2011 or just some ... and so on. In the latest CTP ( http://blogs.msdn.com/b/astoriateam/archive/2011/06/30/announcing-wcf-data-services-june-2011-ctp-for-net4-amp-sl4.aspx ) There is support for all operators who should allow you to do what you want. See this blog post for more details: http://www.odata.org/blog/even-more-any-and-all .

+2
source

Yes, it should be possible. You need to use something like this.

~/Student()?$expand=Course/Period&$filter=Course/any(d:d/Period/Year eq 2011) 

Course/any() checks if something in the collection matches the expression in () .

d: specifies the iterator for the collection, which then refers to d/Period/Year .

The link is given in the OData Documentation in Section 5.1.1.10.1 (Search "/ any").

Note. You can also do / all so that all courses meet certain criteria.

0
source

All Articles