0
votes

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"}}]}

2
Are you looking for "/Products?$expand=ProductDetails($select=Name)"? - Marvin Smit
Yes, but this does not work as ProductDetails is not a navigation property. - Ankit
Looks like: Should work with $expand, but 'not supported' is thrown by the implementation. That's a V3 ref though. stackoverflow.com/questions/20137681/… - Marvin Smit

2 Answers

1
votes

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.

0
votes

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