我正在构建一个 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."
有人知道如何解决这个问题吗?