Search code examples
odata

How to filter by OData nested dynamic property?


I have an open type entity that is accessed by foo.net/items. The entity is defined as:

<EntityType Name="myEntity" OpenType="true">
    <Key>
        <PropertyRef Name="name" />
    </Key>
    <Property Name="name" Type="string" />
</EntityType>

An entity instance may look something like this:

{
  "name": "foo",
  "location": {
    "country": "USA"
  }
}

How can I reference the country property in my filter odata option? Is this the correct approach? foo.net/items?$filter=location/country ne null and location/country eq 'USA'.

I believe the above is correct because it is a valid way to reference a property as per the ABNF in OData V4.


Solution

  • Yes, you can reference nested property by parentProperty/childProperty.

    In your case is not necessary to check if nested property in not null, so you can simplify the query like this:

    foo.net/items?$filter=location/country eq 'USA'