如何过滤属性不存在的 OData 集合?

How to filter OData collection where attribute does not exist?

我有一个 OData 集合,其中的数据如下所示:

{
    "@odata.context": "http://localhost:5488/odata/$metadata#folders",
    "value": [
        {
            "name": "samples",
            "_id": "79a91bc9-9083-4442-ac8d-ad30777ac8c8",
            "creationDate": "2019-08-05T04:39:00.670Z",
            "modificationDate": "2019-08-05T04:39:00.670Z",
            "shortid": "18xQnNv"
        },
        {
            "name": "Population",
            "folder": {
                "shortid": "18xQnNv"
            },
            "_id": "7406269b-669c-41ce-92f3-f540792df07e",
            "creationDate": "2019-08-05T04:39:00.750Z",
            "modificationDate": "2019-08-05T04:39:00.750Z",
            "shortid": "0ppeLV"
        },
        {
            "name": "Invoice",
            "folder": {
                "shortid": "18xQnNv"
            },
            "_id": "525aff6a-6b10-4ad6-93ce-e9c753e8ade0",
            "creationDate": "2019-08-05T04:39:00.790Z",
            "modificationDate": "2019-08-05T04:39:00.790Z",
            "shortid": "G3i2B3"
        },
        {
            "name": "Default",
            "_id": "58daf5aa-1f13-4ff9-be1f-8cb11a812485",
            "creationDate": "2019-08-07T22:56:45.160Z",
            "modificationDate": "2019-08-07T22:56:45.160Z",
            "shortid": "Sm8LpmP"
        }
    ]
}

我想排除具有属性 "folder" 的对象。我试过使用 GET 请求:http://localhost:5488/odata/folders?$filter=folder eq null 但没有成功。这甚至可能吗?有没有办法像这样过滤我的请求?

您也许可以使用 the all lambda operator 来完成此操作。 operator all 在空集合上总是 return true。因此,如果您设置一个条件,即没有实际存在的文件夹属性将永远评估为 true,那么结果 应该 仅筛选那些具有空属性的对象。

这只是一个理论。您需要进行测试,但您的样本可能看起来像这样。

http://localhost:5488/odata/folders?$filter=folder/all(f:f/shortid eq 'xxxxxx')

您没有提到您使用的 OData 版本,但 lambda 表达式至少是 V4 及更高版本。可能更早,不确定。