微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 如何使用OData对Dictionary进行过滤?

我的Controller上有一个OData查询启用操作,返回一个资产.

C#模型.

var asset = new Asset()
{
    Id = Guid.NewGuid().ToString(),Name = "Cool Asset Yo",Url = "http://test/test.asset",Tags = new[] {"test"},Properties = new Dictionary<string,string>
    {
        {"platform","android"},{"dim_depth","1.0"},{"dim_height",{"dim_width",{"item_type","Trim"}
    }
}

返回JSON

[
      {
        "name": "Cool Asset Yo","properties": {
          "platform": "android","dim_depth": "1.0","dim_height": "1.0","dim_width": "1.0","item_type": "Trim"
        },"tags": [
          "test"
        ],"url": "http://test/test.asset","id": "77d9b9df-4f4b-4fad-a1d3-af5075d52a62",}
 ]

示例查询有效!

> api / Asset?$filter = startswith(name,’Cool’)
> api / Asset?$filter = tags / any(tag eq’test’)
> api / Asset?$filter = id eq’77d9b9df-4f4b-4fad-a1d3-af5075d52a62′

现在失败:-(

> api / Asset?$filter = properties / platform eq’Android’

>错误属性“platform”的属性访问的父值不是单个值.属性访问只能应用于单个值.

> api / Asset?$filter = properties / any(道具:道具/平台eq’Android’)

>错误:无法在类型’System.Collections.Generic.keyvaluePair_2OfString_String’上找到名为’platform’的属性.

> api / Asset?$filter = properties / any(keyvalue:keyvalue(‘platform’)eq’Android’)

>错误:找到名为’keyvalue’的未知函数.这也可能是导航属性上的函数导入或键查找,这是不允许的.

> api / Asset?$filter = properties / any(keyvalue:keyvalue eq’Android’)

>错误:检测到具有不兼容类型的二元运算符.找到操作数类型’System.Collections.Generic.keyvaluePair_2OfString_String’和’Edm.String’,用于运算符类’Equal’.

> api / Asset?$filter = properties [‘platform’] eq’Android’

>错误:’properties [‘platform’] eq’Android”中第31位的语法错误.

如何获得“Android”平台的资产列表?我看到在模型中使用的Microsoft Documents通用字典中的示例,我没有看到任何$filter示例.

解决方法

在您的方案中,“属性”看起来是字典属性,但字典属性不是OData中的内置属性.

此外,您的有效负载看起来是正常的JSON序列化输出.这不是odata有效载荷.

你说你看过Microsoft Documents of Generic Dictionaries being used in a model的例子,它是动态属性用法.请注意“你的场景(字典)和动态属性之间存在差异”.

最重要的是,Web API OData现在支持过滤动态属性.
请参阅 commit 中的我的测试用例

希望它可以帮到你.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐