2

I'm trying to select the node where empUID equals a certain ID. Here is my JSON snippet

{
    "dsShedule": {
        "ttEmployee": [
            {
                "empUID": 2649,
                "empNameFirst": "Firstname",
                "empNameLast": "lastName",
                "empFunction": "AFWERKER DRUKKERIJ",
                "ttShedule": [
                    {
                        "UID": 47,
                        "empUID": 2649,
                        "datStart": "2013-05-20",
                        "datStop": "2013-05-20",
                        "regime": 1,
                        "state": "PLANNED",
                        "ttSheduleDay": [
                            {
                                "SheduleUID": 47,
                                "dat": "2013-05-20",
                                "dateTimeStart": "2013-05-20T08:00:00.000",
                                "dateTimeStop": "2013-05-20T17:00:00.000",
                                "duration": 8
                            }
                        ]
                    },
                    {
                        "UID": 57,
                        "empUID": 2649,
                        "datStart": "2013-05-21",
                        "datStop": "2013-05-21",
                        "regime": 1,
                        "state": "PLANNED",
                        "ttSheduleDay": [
                            {
                                "SheduleUID": 57,
                                "dat": "2013-05-21",
                                "dateTimeStart": "2013-05-21T08:00:00.000",
                                "dateTimeStop": "2013-05-21T17:00:00.000",
                                "duration": 8
                            }
                        ]
                    }
                ]
            },

I'm able to select all the employee nodes but the moment I try to select the node where the ID equals for example 494323, it can not be found.

The function

        JObject jObj = JObject.Parse(json);
        int firstUID = 494323;
        var linq = jObj["dsShedule"]["ttEmployee"].Select(x => new
                        {
                            empUID = x.SelectToken("empUID"),
                            empNameFirst = x.SelectToken("empNameFirst"),
                            empNameLast = x.SelectToken("empNameLast"),
                            ttShedule = x.SelectToken("ttShedule")
                        });
        var uid = linq.Where(x => x.empUID.Equals(firstUID));

I'm using VS2012 and when I debug the element linq and look for the value of empUID, it shows the value {494323} (Why the brackets?).

Below a picture of the variables:

enter image description here

As you can see uid is empty, what am I missing?

Thanks in advance

4

1 回答 1

3

您选择的不是选择查询中的值,而是 JToken 对象:

empUID = x.SelectToken("empUID")

empUID 是 JToken 类型(您可以在调试视图中看到类型)。然后您尝试将整数与将失败的 JToken 进行比较。

只需选择如下值:

empUID = x.SelectToken("empUID").Value<int>()

或者Value<T>比较时使用:

var uid = linq.Where(x => x.empUID.Value<int>() == firstUID);

编辑

我不确定你到底想要什么,但这应该给你一个想法。

  1. 过滤 ttEmployee 数组以查找具有给定 empUID 的第一个条目
  2. 选择该条目的 ttShedule 数组
  3. 过滤Where所有 ttSchedule 条目,这些条目包含具有给定值的 dat 属性的 ttSheduleDay:

    var linq = jObj["dsShedule"]["ttEmployee"]
             // first filter for a single emp by empUID
             .First(emp => emp["empUID"].Value<int>() == firstUID)
             // then select the ttShedule array of that emp
             .Select(emp => emp["ttShedule"])
             // now filter for whatever ttShedule you need
             .Where(shed => shed["ttSheduleDay"]
                          .Any(day => day["dat"].Value<DateTime>() 
                                                 == new DateTime(2013, 5, 24))
    
于 2013-05-23T12:32:48.263 回答