简体   繁体   中英

Linq to Sql One to many relationship string.join

For a single book in have multiple authors.I want to fetch the values in json format I get json as

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

But I need to get it in

"AuthorName":Author1,Author2  

format.Can I achieve that? here is my query

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);

Try selecting a string instead of an anonymous object, like this:

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM