我正在尝试根据数组或值的第一个元素选择 roleId 为 4 的所有用户。我怎样才能做到这一点?另外,我怎样才能显示roleId?这是我的 JSON 字符串:
{
"?xml" : {
"@version" : "1.0",
"@encoding" : "UTF-8"
},
"DataFeed" : {
"@FeedName" : "AdminData",
"People" : [{
"id" : "63",
"active": "1",
"firstName" : "Joe",
"lastName" : "Schmoe",
"roleIds" : {
"int" : "4"
}
} , {
"id" : "65",
"active": "1",
"firstName" : "Steve",
"lastName" : "Jobs",
"roleIds" : {
"int" : ["4", "16", "25", "20", "21", "22", "17", "23", "18"]
}
} , {
"id" : "66",
"active": "1",
"firstName" : "Bill",
"lastName" : "Gates",
"roleIds" : {
"int" : ["3", "16", "25", "20"]
}
}
]
}
}
这是我正在使用的查询:
JObject jsonFeed = JObject.Parse(jsonString);
from people in jsonFeed.SelectTokens("DataFeed.People").SelectMany(i => i)
let ids = people["roleIds.int"]
where (int) people["active"] == 1 &&
(ids.Type == JTokenType.Array) ?
((int[]) ids.ToObject(typeof(int[]))).Any(k => k == 4) :
// <-- this looks through all items in the array.
// I just want to compare against the 1st element
(int) ids == 4
select new {
Id = (int) people["id"],
ResAnFName = (string) people["firstName"],
ResAnLName = (string) people["lastName"],
RoleId = ?? // <-- how do I get the roleID displayed
});
我收到以下错误((int[]) ids.ToObject(typeof(int[]))).Any(k => k == 4)
:
NullReferenceException:对象引用未设置为对象的实例。
最后,我的结果应该是:Joe Schmoe
& Steve Jobs
,只有。
我究竟做错了什么?