Answer the question
In order to leave comments, you need to log in
How to get filtered data from many-to-many table?
Here is the most simplified example. There are 3 models
public class Book
{
public int Id { get; set; }
[Required]
public string Title { get; set; }
public List<AuthorBook> AuthorsBooks { get; set; } = new List<AuthorBook>();
}
public class Author
{
public int Id { get; set; }
[Required]
public string Name { get; set; }
public List<AuthorBook> AuthorsBooks { get; set; } = new List<AuthorBook>();
}
public class AuthorBook
{
public int AuthorId { get; set; }
public Author Author { get; set; }
public int BookId { get; set; }
public Book Book { get; set; }
}
public IEnumerable<Book> GetBooksByAuthor(int authorId)
=> context.Books
.Include(book => book.AuthorsBooks
.Where(authorBook => authorBook.AuthorId == authorId))
.ThenInclude(authorBook => authorBook.Person)
.Where(book => book.AuthorsBooks.Count > 0);
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question