我无法通过odata查询来计算嵌套元素

时间:2019-06-28 22:45:53

标签: c# .net odata asp.net-core-webapi core

我正在尝试对odata查询中的相关对象进行计数,但是我没有得到汇总结果,只有详细信息

如果我算第一个可以正常工作的对象类型...

.../ObjectType1?$apply=groupby((ObjectType1Type),aggregate(Id with countdistinct as total))

我得到了预期的结果...

[
    {
        "ObjectType1Type": "Type1",
        "total": 15
    },
    {
        "ObjectType1Type": "Type2",
        "total": 1
    }
]

如果我算第二种可以正常工作的对象类型...

.../ObjectType2?$apply=groupby((ObjectType2Type),aggregate(Id with countdistinct as total))

我得到了预期的结果...

[
    {
        "ObjectType2Type": "Type1",
        "total": 13
    },
    {
        "ObjectType2Type": "Type2",
        "total": 15
    },
    {
        "ObjectType2Type": "Type3",
        "total": 13
    },
    {
        "ObjectType2Type": "Type4",
        "total": 15
    }
]

如果我要得到一个对象并计算所有与其相关的对象...

.../ObjectType1(Id)?$expand=ObjectType2($apply=groupby((ObjectType2Type),aggregate(Id with countdistinct as total)))

我得到了,我不希望...

{
    "@odata.context": "https://localhost:44391/v1.0/odata/$metadata#Objects1(Objects2())/$entity",
    "Id": "50e445b9-ad81-4919-b7b9-4b11b67277d5",
    "ObjectType1": "Type1",
    "Name": "My Object",
    "Timestamp": "2018-11-14T16:26:28+0000",
    "Message": "This is the message",
    "ObjectType2": [
        {
            "Id": "6778883d-df2a-4ec6-adaf-fcd05d7f4a5f",
            "ObjectType2Type": "Type1"
        },
        {
            "Id": "19d46eb7-c0dc-478c-a522-9b0c928b29c4",
            "ObjectType2Type": "Type1"
        },
        {
            "Id": "79cffedd-2243-4725-bfd5-a592ff599fa5",
            "ObjectType2Type": "Type1"
        },
        {
            "Id": "e119dd31-8521-45c4-b4da-8fab691cdd93",
            "ObjectType2Type": "Type1"
        },
        {
            "Id": "9a188e60-30ed-4b77-a358-e75ffc829362",
            "ObjectType2Type": "Type1"
        },
        {
            "Id": "0b06e260-87ef-42b9-87c4-bf8e63395a36",
            "ObjectType2Type": "Type1"
        },
        {
            "Id": "d5a6ef17-710c-4761-b5d0-7f609ac07f1a",
            "ObjectType2Type": "Type1"
        },
        {
            "Id": "3133d38d-84a3-4502-b661-3a8ecdc4a5c9",
            "ObjectType2Type": "Type1"
        },
        {
            "Id": "89760715-86fd-4e8d-98e6-b755854ce1e7",
            "ObjectType2Type": "Type1"
        }
    ]
}

我希望以下... (根据Odata definition

{
    "@odata.context": "https://localhost:44391/v1.0/odata/$metadata#Nodes(Relationships())/$entity",
    "Id": "50e445b9-ad81-4919-b7b9-4b11b67277d5",
    "ObjectType1": "Type1",
    "Name": "My Object",
    "Timestamp": "2018-11-14T16:26:28+0000",
    "Message": "This is the message",
    "ObjectType2": [
        {
            "ObjectType2Type": "Type1",
            "total": 9
        }
    ]
}

这可行吗? 我想念什么?

相关nuget的版本是...

  • “ Microsoft.AspNetCore.Mvc.Versioning” Version =“ 3.0.0”
  • “ Microsoft.AspNetCore.OData” Version =“ 7.1.0”
  • “ Microsoft.AspNetCore.OData.Versioning” Version =“ 3.0.0”
  • “ Microsoft.OData.Core” Version =“ 7.6.0”

0 个答案:

没有答案