Inx
Inx

Reputation: 2384

OData WebAPI querying children and scoping

Im having the following output from a very simple Web API-project, and I want to filter out all "entities" that doesnt have the Children-property set as null.. is there anyway to do this?.. what would the OData-Query be?

The next question is.. is it possible to create a query which returns all "Parents" with a child which name starts with Emma?

And the final question.. is it possible to scope down to properties with in the "Parent".. for instance I might want to create a query based on the age of the "parent" but I then only want to see the children for that parent.. where "scoping" down so the ouput only shows the children would be quite a neat feature..

Here is my url: {mydomain}/api/persons/Children$filters=Children

Here is my XML:

<ArrayOfPerson xmlns:i="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://schemas.datacontract.org/2004/07/MvcApplication5.Models">
<Person>
<Age>12</Age>
<Children i:nil="true"/>
<Name>Ada Svensson</Name>
</Person>
<Person>
<Age>19</Age>
<Children i:nil="true"/>
<Name>Emma Svensson</Name>
</Person>
<Person>
<Age>43</Age>
<Children>
<Person>
<Age>12</Age>
<Children i:nil="true"/>
<Name>Ada Svensson</Name>
</Person>
<Person>
<Age>19</Age>
<Children i:nil="true"/>
<Name>Emma Svensson</Name>
</Person>
</Children>
<Name>Bertil Svensson</Name>
</Person>
<Person>
<Age>34</Age>
<Children i:nil="true"/>
<Name>Kalle Persson</Name>
</Person>
</ArrayOfPerson>

Thanks in advance!

Upvotes: 1

Views: 1742

Answers (1)

Vitek Karas MSFT
Vitek Karas MSFT

Reputation: 13310

If your service supports OData V3 then you can do

/persons?$filter=Children/any()

(Note that if Children is a collection then it can never be null, it can only be empty - as per OData rules).

Currently it's not possible to filter on parent and then only request children using plain OData. You can filter on parent and then get both parent and children for it:

/persons?$filter=<my parent condition>&$expand=Children.

Upvotes: 3

Related Questions