2016-05-20 5 views
0

次のような単純なOData 4コントローラ(下記参照)を使用すると、都市を選択すると、ちょうどの都市が選択されますか?

{ 
    "@odata.context":"http://localhost/api/$metadata#Customers(Location)","value":[ 
    { 
     "Location":{ 
     "Country":"Ireland","City":"Sligo" 
     } 
    },{ 
     "Location":{ 
     "Country":"Finland","City":"Helsinki" 
     } 
    } 
    ] 
} 

しかし、私はちょうど都市を取得するように、1より深いをドリルダウンする方法がわからない:

http://localhost//api/Customers?$select=Location

は私を与えます。これも可能ですか?

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; 
    } 
} 

答えて

1

$selectのための文法はLocation/Cityのようなパス式を許可していません。最適な方法は、CustomersエンティティセットにバインドされたOData関数を定義することです。例えば、

[HttpGet] 
public IEnumerable<string> GetCities() 
{ 
    return customers.Select(c => c.Location.City); 
} 

そして、次のようにそれを呼び出す:

GET http://localhost/api/Customers/ServiceNamespace.GetCities 
+0

おかげで、それは私が最後にやってしまったものです。 –

関連する問題