开发者

Load parent and child table in one query linq to entitiy

I have a following tables/classes str开发者_如何学Cucture in Linq to entities.

Books
{
 bookId,
 Title
}

Tags
{
  TagId
  Tag
}

BooksTags
{
 BookId
 TagId
}

Now I need to write a query which gives me result like this

Class Result
{
 bookId,
 Title,
 Tags
}

Tags should be comma separated text from the tags table by joining all three tables. How to get it done.

Thanks

Parminder


(from b in books
select new { BookId = b.BookId,
             Title = b.Title,
             Tags = bookTag.Where(bt => bt.BookId == b.BookId).Select(bt => tags.Single(t => t.TagId == bt.TagId).TagTitle)
           }).ToList().Select( t => new 
           { BookId = b.BookId,
             Title = b.Title,
             Tags = string.Join(", ", t.Tags.ToArray())
           }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜