Here is the setup: I've got a "Student" who has a related entity "Course" (1 to many). Each "Course" has a related entity "Period" that contains all of the time and date details of the course. I want to get back all of the students who have had a course in 2011.
I've tried this:
~/Student()?$expand=Course/Period&$filter=Course/Period/Year eq 2011
but it results in an error: No property 'Period' exists in type 'System.Data.Objects.DataClasses.EntityCollection`1[[Course]]
Clearly, Period is being treated as a property rather than an Entity, but I'm confused, because the following query does return the results I expect, and it uses almost the same syntax:
~/Student()?$expand=Course/Period&$select=Course/Period/Year
So am I doing something wrong with the $filter syntax, or is this not possible?
TIA for any insight.
The filter would work if the navigation property would be a singleton, but since it's a collection (1 to many) the filter won't work. Mainly because it's unclear what would that mean. Do you want students which 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's a support for any and all operators which should allow you to do what you want. For more details see this blog post: http://www.odata.org/blog/even-more-any-and-all.
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()
looks to see if anything in the collection matches the expression in ()
.
d:
specifies an iterator for the collection which is then referenced in the d/Period/Year
.
Reference can be found in OData Documentation In Section 5.1.1.10.1 (Search for "/any").
Note: You can also do /all to ensure that all courses match some criteria.