Linq to Sql一对多关系string.join

时间:2014-06-03 15:53:39

标签: c# json linq

对于有多位作者的单本书。我想以json格式获取值 我得到json为

"AuthorName":"{ AuthorName = Author1 },{ AuthorName =Author2  }",

但我需要在

中得到它
"AuthorName":Author1,Author2  

format.Can我实现了吗?这是我的查询

var jsonData = from w in bookData
                           join b in barcodes on w.Id equals b.BookId
                           select new 
                           {
                           w.AccessionNo,
                           AuthorName=string.Join(",", from a in bookAuthor
                                          where a.BookShelfId == w.Id
                                          select new {
                                           a.Authors.AuthorName
                           }),
                        w.BookInfoId,                               
                           };
            return Json(jsonData, JsonRequestBehavior.AllowGet);

1 个答案:

答案 0 :(得分:1)

尝试选择字符串而不是匿名对象,如下所示:

var jsonData = from w in bookData
                       join b in barcodes on w.Id equals b.BookId
                       select new 
                       {
                       w.AccessionNo,
                       AuthorName=string.Join(",", from a in bookAuthor
                                      where a.BookShelfId == w.Id
                                      select a.Authors.AuthorName
                       ),
                    w.BookInfoId,                               
                       };
        return Json(jsonData, JsonRequestBehavior.AllowGet);