2015-09-21 70 views
1

我正在構建一個OData應用程序,我在如何檢索結果以及只包含某些(子屬性)方面苦苦掙扎。如何僅選擇OData子元素

首先,讓我告訴你在我的建設者登記:

builder.EntitySet<AggregatedArticlesSearchModel>("Search").EntityType.HasKey(x => x.Name); 

現在,就來我是從我的查詢返回的模式:

<EntityType Name="AggregatedArticlesSearchModel"> 
    <Key> 
     <PropertyRef Name="Name"/> 
    </Key> 
    <Property Name="Name" Nullable="false" Type="Edm.String"/> 
    <Property Name="Values" Type="Collection(Zevij_Necomij.Mobile.App.Api.Models.OccurenceViewModel)"/> 
</EntityType> 
<ComplexType Name="OccurenceViewModel"> 
    <Property Name="Value" Type="Edm.String"/> 
    <Property Name="Count" Nullable="false" Type="Edm.Double"/> 
    <Property Name="Articles" Type="Collection(Zevij_Necomij.Mobile.App.Api.Models.AggregatedArticleDescriptionViewModel)"/> 
</ComplexType> 
<ComplexType Name="AggregatedArticleDescriptionViewModel"> 
    <Property Name="Name" Type="Edm.String"/> 
    <Property Name="Specification" Type="Edm.String"/> 
    <Property Name="Brand" Type="Edm.String"/> 
</ComplexType> 

當我執行請求獲取數據,我沒有做任何事情,只是從數據庫返回結果:

public async Task<IHttpActionResult> Get() 
{ 
    // Create all the managers for the platform context that are required by the application. 
    var classificationManager = Context.CreateManager(typeof(AggregatedArticleManager<>)) as AggregatedArticleManager<IAggregatedArticleStore<AggregatedArticle>>; 

    var classifications = await classificationManager.GetAllAsync(); 

    var returnList = classifications.OrderBy(x => x.Name).Select(AggregatedArticlesSearchModel.MapFromDbModel).ToList(); 

    return Ok(returnList.AsQueryable()); 
} 

由於我與子對象的工作,該列表可以得到相當巨大:

{ 
    "@odata.context":  "http://api.mobileapp.appserver.dev.dsoft.be/OData/$metadata#Search", 
    "value": [ 
    { 
     "Name": "(Veiligheids)slipkoppeling", 
     "Values": [ 
     { 
      "Value": "ja", 
      "Count": 118, 
     "Articles": [ 
     { 
      "Name": "230 V Sleuvenzaag", 
      "Specification": "Compacte machine", 
      "Brand": "Makita" 
     }, 
     { 
      "Name": "230V Cirkelzaag SJS", 
      "Specification": "Softstart voor 
     }, 
    } 
} 

我可以有千餘條在一組,因此,辦法不多回到通過Web API。 因爲我不需要在單個請求所有這些特性,我想,讓客戶端只使用?$select參數,因此客戶檢索子屬性可以說,例如:

OData/Search?$select=Values 

這裏的問題是,我舉例來說,只想要回計數,因此,我因子評分,像這樣的請求是可能的:

OData/Search?$select=Values/Count 

然而,這導致了OData的錯誤:"The query specified in the URI is not valid. Found a path with multiple navigation properties or a bad complex property path in a select clause. Please reword your query such that each level of select or expand only contains either TypeSegments or Properties."

有誰知道如何解決這個問題的人?

回答

2

@Complexity

據我所知,不支持在屬性中選擇子屬性。

但是,如果您將OccurenceViewModel構建爲實體類型,則可以使用$ expand中的嵌套$ select來滿足您的要求。

例如:

1)構建實體類型

builder.EntitySet<OccurenceViewModel>("ViewModels").EntityType.HasKey(x => x.Value); 

然後,在ValuesAggregatedArticlesSearchModel應該是一個導航屬性。

2)現在,你可以按如下只返回Count屬性發出GET請求:

GET ~/odata/Search?$select=Values&$expand=Values($select=Count) 

然後,有效載荷應該像下面的:

{ 
    "@odata.context":"http://localhost/odata/$metadata#Search(Values,Values(Count))","value":[ 
    { 
     "Values":[ 
     { 
      "Count":101.0 
     },{ 
      "Count":102.0 
     } 
     ] 
    },{ 
     "Values":[ 
     { 
      "Count":101.0 
     },{ 
      "Count":102.0 
     } 
     ] 
    } 
    ] 
} 

希望其能幫你。謝謝。

+0

謝謝,我會看看並回來看看它是否正常工作。 – Complexity