给定以下简单的OData 4控制器(请参阅下文),如何仅$select城市?
http://localhost//api/Customers?$select=Location
给我:
{
"@odata.context":"http://localhost/api/$metadata#Customers(Location)","value":[
{
"Location":{
"Country":"Ireland","City":"Sligo"
}
},{
"Location":{
"Country":"Finland","City":"Helsinki"
}
}
]
}
但我不知道如何更深入地钻取,以便我得到城市。这可能吗?
public class CustomersController : ODataController
{
private List<Customer> customers = new List<Customer>()
{
new Customer
{
CustomerId = 1,
Location = new Address
{
City = "Sligo",
Country = "Ireland"
}
},
new Customer
{
CustomerId = 2,
Location = new Address
{
City = "Helsinki",
Country = "Finland"
}
}
};
[EnableQuery]
public List<Customer> Get()
{
return customers;
}
}
$select
的语法不允许像 Location/City
这样的路径表达式。最好的办法是定义绑定到Customers
实体集的 OData 函数。例如,
[HttpGet]
public IEnumerable<string> GetCities()
{
return customers.Select(c => c.Location.City);
}
并按如下方式调用它:
GET http://localhost/api/Customers/ServiceNamespace.GetCities