Tim
Tim

Reputation: 3038

OData: Could not find a property named on inherited type

For my example, I have two classes

public class Location
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string Address3 { get; set; }
    public string Town { get; set; }
    public string County { get; set; }
    public string CountryCode { get; set; }
}

public class Customer : Location
{
    public string BankAccountNumber { get; set; }
    public string BankSortCode { get; set; }
}

In my query, I am returning all the locations and customers.

http://localhost:80/odata/Location?select=Id,Name,Town

However if I try to select anything in the customer (edit: So I want all locations, but bank account numbers if the location is also a customer), I get an error.

http://localhost:80/odata/Location?select=Id,Name,Town,BankAccountNumber

"The query specified in the URI is not valid. Could not find a property named 'BankAccountNumber' on type 'MyNamespace.Location'."

Is there any way to select the field in inheriting types, without selecting all? Thanks.

Upvotes: 2

Views: 9577

Answers (2)

Marco
Marco

Reputation: 23945

According to OData.org, there are 2 options to query a derived type:

  1. ~/Location!Customer/
  2. ~/Location/OfType('Customer')

So your query should look like this:

http://localhost:80/odata/Location!Customer?select=Id,Name,Town,BankAccountNumber

or

http://localhost:80/odata/Location/OfType('Customer')?select=Id,Name,Town,BankAccountNumber

/EDIT: QianLi pointed out, that the above blog entry refers to OData V2. In Odata4 inherited types are accessed in the following syntax:

http://host/service/BaseType/Model.SubType

Reference: http://docs.oasis-open.org/odata/odata/v4.0/os/part2-url-conventions/odata-v4.0-os-part2-url-conventions.html#_Toc372793786

Upvotes: 3

QianLi
QianLi

Reputation: 1098

You should add not only the Type name but also the namespace of the type. For example: http://services.odata.org/TripPinWebApiService/People('russellwhyte')/Trips(1001)/PlanItems/ODataSamples.WebApiService.Models.Flight?$select=StartsAt

The type Flight inherits from the type PlanItem. And ODataSamples.WebApiService.Models is the namespace.

More detailed information of derived type, you can refer to http://www.odata.org/getting-started/advanced-tutorial/ with some live example if you find the spec too long to read...

Upvotes: 2

Related Questions