如何使用嵌套在列表中的动态数据中的Odata进行过滤?

时间:2015-07-10 15:17:20

标签: c# json dynamic filter odata

json文件的例子

{
    "ProjectName": "14010-00 General Expenses",
    "ClientName": "Whiting",
    "Created": "2015-06-26T21:06:17.721Z",
    "Data": {
      "function": "LIT",
      "tag": "1461",
      "rack": "1",
      "slot": "4",
      "channel": "1"
    }
},
{
    "ProjectName": "104021-00 CPF",
    "ClientName": "Bonanza Creek",
    "Created": "2015-06-26T21:03:20.732Z",
    "Data": {
      "tag": "200",
      "function": "FT",
      "basemodel": "FT-200",
      "shortdescription": ""
    }
}

我希望根据数据的内容进行过滤,但是不同组件之间的差异很大。所以Component.cs文件拥有

public class Component{
    public string ClientName{get; set;}
    public string ProjectName{get; set;}
    ...
    public IDictionary<string, object> Data { get; set; }
}

问题可能来自字典由字符串,对象而不是字符串组成,字符串但是大多数(如果不是全部)数据的值仍然是字符串。

所以

~/component?$filter=ClientName eq 'Whitning'

会奏效;但是,查询失败

~/component?$filter=Data/function eq 'FT'

并给出错误响应     &#34;属性&#39;功能&#39;的属性访问的父值。不是单一的价值。属性访问只能应用于单个值。&#34; 并尝试

~/component?$filter=Data/any(d: d/function eq 'FT')

给出错误响应 &#34;无法找到名为&#39; function&#39; on type&#39; System.Collections.Generic.KeyValuePair_2OfString_Object&#39;。&#34;

那么如何根据Data对象中的对象进行过滤,这些对象是动态创建的并且不总是存在?

1 个答案:

答案 0 :(得分:0)

您使用的是Web API OData吗?如果是,Data只是动态属性的容器。它不应该存在于有效载荷中。所以你的有效载荷应该是:

{
    "Id": "558dbec9940bc716801a95d4",
    "FacilityName": "Ray",
    "ProjectName": "14010-00 General Expenses",
    "ClientName": "Whiting",
    "ProjectNames": [],
    "Name": "IoList",
    "Created": "2015-06-26T21:06:17.721Z",
    "Updated": "2015-06-26T21:06:17.721Z",
    "function": "LIT",
    "tag": "1461",
    "rack": "1",
    "slot": "4",
    "channel": "1"
},{ ...