linq.js 意想不到的结果
linq.js unexpected results
好的,所以我得到了这个名为 "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 = '√';
}
但是当我执行这段代码时,我得到了以下结果...
为什么它不只是 return 故障来自我的匹配 RoomId
的对象?我确定 ID 确实匹配。我在这里做错了什么,我真的被这个困住了...
提前致谢!
回答问题
Why won't it just return the fault's from my object with the matching RoomId? I'm sure the Id's do match. What am I doing wrong here, i'm getting really stuck over this...
您需要添加 .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>
好的,所以我得到了这个名为 "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 = '√';
}
但是当我执行这段代码时,我得到了以下结果...
为什么它不只是 return 故障来自我的匹配 RoomId
的对象?我确定 ID 确实匹配。我在这里做错了什么,我真的被这个困住了...
提前致谢!
回答问题
Why won't it just return the fault's from my object with the matching RoomId? I'm sure the Id's do match. What am I doing wrong here, i'm getting really stuck over this...
您需要添加 .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>