1

好的,所以我得到了这个名为“Faults”的 JSON 对象

"Faults":[{"RoomId":1,"ElementId":173,"FaultTypeId":1,"Count":1,"Remark":""},{"RoomId":3,"ElementId":211,"FaultTypeId":7,"Count":1,"Remark":""},{"RoomId":4,"ElementId":173,"FaultTypeId":1,"Count":1,"Remark":""}]

调试器中显示的故障对象:

调试器中显示的故障

现在我需要使用 he 检查房间是否包含故障RoomId。我为此使用的代码是:

    Enumerable.From(audit.Rooms).ForEach(function(room, index) {//√
    var containsFaults = '';

    //room.Id is ALWAYS filled, it can't be null
    var test1 = faults.Select("$.RoomId==" + room.Id).Count();
    var test2 = faults.Select("$.RoomId==" + room.Id);

    if (faults.Select("$.RoomId==" + room.Id).Count() > 0) {
        containsFaults = '√';
    }

但是当我执行这段代码时,我得到以下结果......

结果打印屏幕

为什么它不只是从我的对象中返回故障并匹配RoomId?我确定 ID 确实匹配。我在这里做错了什么,我真的被困在这个...

提前致谢!

4

1 回答 1

1

回答问题

为什么它不只是从我的对象中返回具有匹配 RoomId 的故障?我确定 ID 确实匹配。我在这里做错了什么,我真的被困在这个...

您需要添加.ToArray()以呈现结果。

var test2 = faults.Select("$.RoomId==" + room.Id).ToArray();
//                                               ^^^^^^^^^^

var audit = { Faults: [{ RoomId: 42, ElementId: 4711, FaultTypeId: 0, Count: 0, Remark: "no fault" }, { RoomId: 1, ElementId: 173, FaultTypeId: 1, Count: 1, Remark: "" }, { RoomId: 3, ElementId: 211, FaultTypeId: 7, Count: 1, Remark: "" }, { RoomId: 4, ElementId: 173, FaultTypeId: 1, Count: 1, Remark: "" }] },
    roomId = 4,
    dataset = Enumerable.From(audit.Faults),
    test1 = dataset.Where("$.Count > 0 && $.RoomId==" + roomId).Count();
    test2 = dataset.Where("$.Count > 0 && $.RoomId==" + roomId).ToArray();

console.log(test1);
console.log(test2);
<script src="https://cdnjs.cloudflare.com/ajax/libs/linq.js/2.2.0.2/linq.js"></script>

于 2017-01-18T14:38:53.583 回答