I have a class Book that has a List of Authors and a class Authors which has a list of books, so many to many relation. Within a bookService I get one book from the database and I want to now get all other books that are by the same author of the book I got first.
This is what I tried :
var book = await context.Books
.Where(b => b.Id == id)
.To<BookDetailsViewModel>()
.FirstOrDefaultAsync();
var sameAuthorBooks = await context.Books
.Where(b => b.Authors
.Any(a => book!.Authors.Any(a2 => a2.Id == a.Id)))
.OrderByDescending(x => x.Id)
.Take(9)
.To<BookViewModel>()
.ToArrayAsync();
I get the book but when i try to get the books with the same Author it returns an empty list. I’m pretty bad with many to many relations so I tried all kinds of queries and none of them worked so I would greatly appreciate if someone can help me out…
Noire is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.