如何仅选择OData子元素 [英] How to select only OData child elements

查看:90
本文介绍了如何仅选择OData子元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建OData应用程序,并且我在如何检索结果上苦苦挣扎,只包含某些(子属性).

I'm building an OData application and I'm struggling on how to retrieve results and only include certain (child properties).

首先,让我向您展示我的构建器中的注册:

First, let me show you the registration in my builder:

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

现在,转到我从查询中返回的模型:

Now, on to the model that I'm returning from my Query:

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

当我执行获取数据的请求时,我没有做任何花哨的事情,而只是从数据库中返回结果:

When I'm executing a request to get the data, I'm not doing anything fancy but just returning the results from the database:

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

由于我正在处理子对象,因此列表可能会变得非常庞大:

Since I'm working with child objects, the list can get quite huge:

{
  "@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参数来检索子属性,因此客户端可以说例如:

I can have a thousand articles in a single set, thus, way to much to return over the Web Api. Since I don't need all those properties in a single request, I was thinking to let the cliënt only retrieve child properties by using the ?$select parameter, thus the cliënts can say for example:

OData/Search?$select=Values

这里的问题是,例如,我只想返回Count,因此,我坚决要求这样的请求是可能的:

The problem here is that I for example, only want to return the Count, thus, I tought that a request like this was possible:

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."

However, this leads to an OData error: "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."

有人对如何解决这个问题有想法吗?

Anyone who has an idea on how to solve this one?

推荐答案

@Complexity

@Complexity

据我所知,不支持在属性中选择子属性.

As I know, to select a sub property in a property is not supported.

但是,如果将OccurenceViewModel构建为实体类型,则可以在$ expand中使用嵌套的$ select来满足您的要求.

However, if you build the OccurenceViewModel as entity type, then you can use the nested $select in $expand to meet your requirement.

例如:

1)建立实体类型

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

然后,AggregatedArticlesSearchModel中的Values应该是导航属性.

then, Values in AggregatedArticlesSearchModel should be a navigation property.

2)现在,您可以发出GET请求,如下所示,仅返回Count属性:

2) Now, you can issue a GET request as follows to only return the Count property:

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

然后,有效负载应如下所示:

Then, the payload should look like the below:

{
  "@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
        }
      ]
    }
  ]
}

希望它可以为您提供帮助.谢谢.

Hope it can help you. Thanks.

这篇关于如何仅选择OData子元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆