Ankit
Ankit

Reputation: 2518

How to access properties of complex type in OData

How can we form an OData query to access the Name property of complex property ProductDetails in the ProductDTO class?

public class ProductDTO
{
    public int Id { get; set; }
    public string ProductName { get; set; }
    public string Description { get; set; }
    public ProductDetails ProductDetails { get; set; }
}

public class ProductDetails
{
    public string Name { get; set; }
    public string Price { get; set; }
    public string Discount { get; set; }
    public string ManufacturedDate { get; set; }
}

This query gives me ProductDetails:

/Products?$select=ProductDetails

{"@odata.context":"http://localhost:59909/$metadata#Products(ProductDetails)","value":[{"ProductDetails":{"Name":"Laptop","Price":"100299","Discount":"1000","ManufacturedDate":"12:01:2016 09:30:875"}}]}

Upvotes: 0

Views: 4097

Answers (2)

Andrey Tagaew
Andrey Tagaew

Reputation: 1593

Try to use $expand clause

In my situation i had to add a complexType first:

builder.ComplexType<BalanceSheet>();

Additionally I found that different query should be used to get the complex type on UI

Instead of calling http://url/api/AccountDetails?$select=name,accountNumber,balance another url should be used: http://url/api/AccountDetails?$select=name,accountNumber,balance&$expand=balance

you can only see complex properties like balance via $expand

Also, important to have $expand feature turned on. To do that add it before you add the edm model:

endpoints.Select().Expand().Filter().OrderBy().Count().MaxTop(10);
            endpoints.MapODataRoute("odata", "odata", this.GetEdmModel());

See details here: https://stackoverflow.com/a/55476645/2050539

Upvotes: 0

TomDoesCode
TomDoesCode

Reputation: 3681

According to this post, this isn't possible for a $select. However, it isn't clear what you are trying to achieve from your question so I thought I would post this in case it helps. For a single object, you can get the value of a nested property like this, here is an example using the TripPin example OData service: http://services.odata.org/V4/TripPinServiceRW/Airports('KLAX')/Location/Address/$value Here, the Location property is a complex type and we are getting just the value of the Address property on that object.

Upvotes: 1

Related Questions