LINQ to JSON:如何获得正确的格式?

时间:2011-09-15 14:11:35

标签: c# .net linq json linq-to-entities

我试图将我的结果从linq查询转换为json 使用json.net

这就是我所做的:

JObject o = JObject.FromObject(new
{
    UserID = from u in model.USER
             select new
             {
                 UserID = u.UserID
             }
});

这是我得到的:

JSON
  id=1
  result={"UserID":[{"UserID":121},"UserID":121},{"UserID":122},{"UserID":123},{"UserID":124}]}

这就是我需要的:

JSON
  id=1
  result={[{UserID:'121'},{UserID:'121'},{UserID:'122'},{UserID:'123'},{UserID:'124'}]}  

我如何完成这项工作?谢谢

1 个答案:

答案 0 :(得分:9)

试试这个:

 JArray a = JArray.FromObject(
                from u in model.USER
                select new
                {
                    UserID = u.UserID
                }
    );