加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 22:48:14 所属栏目:百科 来源:网络整理
导读:我的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 Dictionarystring,string { {"
我的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 中的我的测试用例

希望它可以帮到你.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读