我正在尝试执行LINQ to JSON的示例代码(如下所述)但是它给了我以下错误
堆栈追踪:
[InvalidOperationException:Lambda参数不在范围内]
我正在执行的代码是:
JObject rss =
new JObject(
new JProperty("id", "James Newton-King"),
new JProperty("name", "http://james.newtonking.com"),
new JProperty("data", "James Newton-King's blog."),
new JProperty("children",
new JArray(
from p in mwsysbot.Software
where p.SoftwareName == name
select new JObject(
new JProperty("id",p.SoftwareUUID),
new JProperty("name", p.SoftwareName)
)
)
)
);
此外,当我删除行“new JProperty(”name“,p.SoftwareName)时”代码执行完美。
为什么?
答案 0 :(得分:3)
我试过这个,它对我有用......
IQueryable<Software> soft = (from s in mwsysbot.Software
select s).ToList();
JObject rss =
new JObject(
new JProperty("id", "James Newton-King"),
new JProperty("name", "http://james.newtonking.com"),
new JProperty("data", "James Newton-King's blog."),
new JProperty("children", new JArray(
from m in soft
select new JObject(
new JProperty("id",m.SoftwareName),
new JProperty("name", m.SoftwareName),
new JProperty("children",new JArray())
)
))
);
我不知道原因!
我们只能在上面使用“List”数据结构吗?
答案 1 :(得分:0)
Linq可能会尝试延迟加载SoftwareName。在创建新对象之前,尝试使用DTO并急切加载参数名称。