复杂的LINQ:嵌套,语句JSON.net和LINQ [英] Complex linq: Nested where statements JSON.net and linq

查看:118
本文介绍了复杂的LINQ:嵌套,语句JSON.net和LINQ的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做到的是选择ttSheduleDay内dateTimeStart。 JSON的下方是一个员工的一个节点,该函数接收三个参数的empUID,日期和值(启动/停止或持续时间)。

What I'm trying to accomplish is to select the dateTimeStart inside the ttSheduleDay. The JSON beneath is a node of one employee, the function receives three parameters an empUID, a date and a value of (start / stop or duration).

我要选择的节点是其中dsShedule> ttEmployee> empUID等于第一个参数,其中ttShedule> ttSheduleDay> DAT等于日期参数和第三个参数,我会与if语句执行。下面的JSON

The node I want to select is where the dsShedule > ttEmployee > empUID equals the first parameter, where the ttShedule > ttSheduleDay > dat equals the date parameter and the third parameter I will execute with an if statement. Below the JSON

{
    "dsShedule": {
        "ttEmployee": [
            {
                "empUID": 2649,
                "empNameFirst": "firstname",
                "empNameLast": "lastname",
                "empFunction": "employee",
                "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
                            }
                        ]
                    }
                ]
            },

在code我已经是选择ttShedule

The code I already have is to select the ttShedule

JObject jObj = JObject.Parse(json);
var linq = jObj["dsShedule"]["ttEmployee"]
                // first filter for a single emp by empUID
                         .First(emp => emp["empUID"].Value<int>() == Convert.ToInt16(empUID))
                         .SelectToken("ttShedule");

有人#2所建议的code是:

The code suggested by someone on Stackoverflow was:

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))

但这个失败的ttShedule的select语句。我想知道我怎么可以扩大我的code选择与第二的dateTimeStart节点if语句。

But this failed on the select statement of ttShedule. I was wondering how i can expand my code to select the dateTimeStart node with the second if statement.

在此先感谢

推荐答案

这里有一种方法,你可以写你的查询:

Here's one way you could write your query:

var employeeId = 2649;
var date = new DateTime(2013, 5, 20);

var query =
    from emp in jObj.SelectToken("dsShedule.ttEmployee")
    where emp.Value<int>("empUID") == employeeId
    let day =
        (from sched in emp["ttShedule"]
        from d in sched["ttSheduleDay"]
        where d.Value<DateTime>("dat") == date
        select d).FirstOrDefault()
    where day != null
    select day.Value<DateTime>("dateTimeStart");

我怀疑你所面临的问题是,与指定的ID的雇员不存在与第一()调用失败。这会不会遇到同样的问题。

I suspect the problem you were facing was that an employee with the specified id did not exist and the First() call failed. This will not run into the same problem.

这篇关于复杂的LINQ:嵌套,语句JSON.net和LINQ的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆