linq.js出人意料的结果

时间:2017-01-18 14:22:48

标签: javascript json linq.js

好的,所以我得到了这个名为"故障"

的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":""}]

调试器中显示的错误对象:

faults shown in debugger

现在我需要使用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 = '√';
    }

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

result printscreen

为什么它只是通过匹配的RoomId从我的对象返回错误?我确定Id的确匹配。我在这里做错了什么,我真的被困在这......

提前致谢!

1 个答案:

答案 0 :(得分: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>
&#13;
&#13;
&#13;