通过Web API OData中的嵌套属性过滤

时间:2018-10-17 13:53:26

标签: c# asp.net-web-api asp.net-web-api2 odata

我在具有嵌套属性的Odata过滤器中遇到一些问题

控制器:

[HTTPGet]
[EnableQuery]
public async Task<IQueryable<MyObj>> GetData()
{
    return await GetUserData();
}

从Web应用输出的获取url请求

GET https://sample.com/IB

[{
        "Id": "1",
        "FirstName": "Alex",
        "School": [
            {
                "SchoolId": "Sc1",
                "Roles": [
                    {
                        "Role": "ROOT",
                    }
                ],
                "Email": "abc@xyz.com"
            }
        ]
    },    
    {
        "Id": "2",
        "FirstName": "Mage",
        "School": [
            {
                "SchoolId": "Sc2",
                "Roles": [
                    {
                        "Role": "Admin",
                    }
                ],
                "Email": "abc@xyz.com"
            }
        ]
    }]

在SchoolId =“ Sc2”的情况下,如何获得学校的成绩

我尝试使用以下网址

GET https://sample.com/IB?$expand=School&$filter=School/SchoolId eq 'Sc2'

但是我收到以下错误消息,

{
    "Message": "The query specified in the URI is not valid. The parent value for a property access of a property 'SchoolId' is not a single value. Property access can only be applied to a single value."
}

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

最近的答复,但您需要使用lambda any运算符进行收集。

https://sample.com/IB?$expand=School&$filter=School/Any (c:c/SchoolId eq 'Sc2')