5

我正在尝试执行 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)”时,代码执行得很好。

为什么?

4

2 回答 2

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”数据结构?

于 2009-06-17T13:47:30.747 回答
0

Linq 可以尝试延迟加载 SoftwareName。在创建新对象之前尝试使用 DTO 并预先加载参数名称。

于 2009-06-15T22:40:40.673 回答