配列または値の最初の要素に基づいて、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
のみ。
私は何を間違っていますか?