我正在構建一個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."
有誰知道如何解決這個問題的人?
謝謝,我會看看並回來看看它是否正常工作。 – Complexity