0
我想寫一個Linq返回加入3個表的結果linq entityframework從連接表中得到結果
有'書','作者'實體。 1書可以有許多作者和1作者可以寫很多書
我想要一個'BooksAuthors'具有書名和書的作者列表的對象。我現在的LINQ(但它返回任何結果,我得到空指針異常)
BooksAuthors = db.tblBooks.SelectMany(
book => book.Authors,
(book, author) => new Books
{
BookID = book.BookID,
BookName = book.BookName,
Authors = book.Authors
}),
其他類:
public class Author
{
public virtual int AuthorID { get; set; }
public virtual string AuthorName { get; set; }
public virtual ICollection<Book> Books { get; set; }
}
public class Book
{
public virtual int BookID { get; set; }
public virtual string BookName { get; set; }
public virtual ICollection<Author> Authors { get; set; }
}
public class Books
{
public int BookID { get; set; }
public string BookName { get; set; }
public IEnumerable<Author> Authors { get; set; }
}
public class LibraryDb : DbContext
{
public DbSet<Book> tblBooks { get; set; }
}