stephen776
stephen776

Reputation: 9234

Linq Query to Search "Many" in one to many relationship

Hey guys I have EF classes of Book and Comments.

A book can have many comments.

How can I search for a book with any comment that contains my search text?

My method looks like this as of now...

public IEnumerable<Book> Search(string commentText)
{

     IQueryable<Book> books = _context.Books;

     books.Where() //need to filter by commentText here

     return books;

}

Upvotes: 1

Views: 2336

Answers (1)

Chandu
Chandu

Reputation: 82913

Try this:

books.Where(a=>a.Comments.Any(b=>b.CommentText.Contains(commentText)));

Upvotes: 5

Related Questions