How can I $ filter the dictionary <string, string> using OData?

I have an action with an enabled OData query on my controller that returns an object.

C # Model.

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", "1.0"},
        {"dim_width", "1.0"},
        {"item_type", "Trim"}
    }
}

Returned 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",
      }
 ]

Examples of queries that work!

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

AND NOW for the crash: - (

  • api/Asset?$filter=properties/platform eq 'Android'
    • Error: The parent value for accessing the properties of the platform property is not the only value. Access to properties can only be applied to one value.
  • api/Asset?$filter=properties/any(props: props/platform eq 'Android')
    • Error: could not find a property with the name "platform" in the type "System.Collections.Generic.KeyValuePair_2OfString_String".
  • api/Asset?$filter=properties/any(keyValue: keyValue('platform') eq 'Android')
    • Error: An unknown function was found with the name "keyValue". It can also be a function import or a key search for a navigation property that is not allowed.
  • api/Asset?$filter=properties/any(keyValue: keyValue eq 'Android')
    • : . "System.Collections.Generic.KeyValuePair_2OfString_String" "Edm.String" "".
  • api/Asset?$filter=properties['platform'] eq 'Android'
    • : 31 'properties [' platform '] eq' Android ''.

"" Android? Microsoft Documents , , $-.

+4
2

"" , OData.

, JSON. .

, Microsoft Documents of Generic Dictionaries, , . , " () ".

, API- OData dynamic. . commit

, .

+1

Sam Xu , OData, . , .

#.

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

JSON

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

, !

api/Asset?$filter=properties/any(keyValue: keyValue/key eq 'platform' and keyValue/value eq '50129486')
0

Source: https://habr.com/ru/post/1688248/


All Articles