Search code examples
filterodataexpand

Avoiding Null value in Odata query


In OData V4, I am trying to expand an object (which is an array) and filter based on if the array object is empty. eg : Request

ODataService/v1/ODataServiceName.svc/v1/ABCRevisions?$expand=Status

Response

{
"@odata.context": "ODataService/v1/ODataServiceName.svc/v1/$metadata#ABCRevisions(Status())",
"value": [
    {
        "Revision": "A",
        "Identifier": "032103",
        "Status": [
            {
                "type": "Status",
                "Name": "Approved"
            },
            {
                "type": "Status",
                "Name": "Released"
            }
        ]
    },
    {
        "Revision": "A",
        "Identifier": "032097",
        "Status": []
    }]}

Basically, I want to filter response where "Status" is not empty

I tried the following it-

a. ODataService/v1/ODataServiceName.svc/v1/ABCRevisions?$expand=Status($filter=Name ne null) This approach didn't work

b. ODataService/v1/ODataServiceName.svc/v1/ABCRevisions?$expand=Status/$Order which gave me the status count but didn't filter the response accordingly Response -

{
"@odata.context": "ODataService/v1/ODataServiceName.svc/v1/$metadata#ABCRevisions(Status())",
"value": [
    {
        "Revision": "A",
        "Identifier": "032103",
        "Status@odata.count": 2        
    },
    {
        "Revision": "A",
        "Identifier": "032097",
        "Status@odata.count": 0
    }]}

Can someone tell me how to filter Object where status is not empty? Regards


Solution

  • I would use any in $filter query to return only entities with at least one status.

    ODataService/v1/ODataServiceName.svc/v1/ABCRevisions?$expand=Status&$filter=Status/any()
    

    or check if count is not equal to 0

    ODataService/v1/ODataServiceName.svc/v1/ABCRevisions?$expand=Status&$filter=Status/$count ne 0