title: EF. Select records from one table based on the properties of the records from second table that have an FK from the first
body: <p>I have two tables.
  BookTitles and BookAuthors. A BookTitle can contain multiple BookAuthors:</p>
  
  <pre><code>BookTitles:
  - Book_Id
  - BookName
  
  BookAuthors:
  - Author_Id
  - AuthorName
  - Book_Id
  </code></pre>
  
  <p>In the second table, the Book_Id is a foreign key. I'm using Entity Framework, database first.
  Here's the model (created automatically) of the BookTitles table:</p>
  
  <pre><code>public partial class BookTitles
  {
      public BookTitles()
      { this.BookAuthors = new HashSet&lt;BookAuthors&gt;(); }
  
      public int Book_Id { get; set; }
      public string BookName { get; set; }
  
      public virtual ICollection&lt;BookAuthors&gt; BookAuthors { get; set; }
  }
  </code></pre>
  
  <p>The last property shows that the object of type BookTitles will contain the list of the BookAuthors  objects, retrieved from the second table.</p>
  
  <p>The question: How do I retrieve the BookTitles records that will  contain, for example, the object of type BookAuthors that will have the BookAuthors.AuthorName == "AAA"?</p>
  
  <p>I tried this but it didn't work:</p>
  
  <pre><code>using (var db = new BooksContext())
  {
      var books =
          db.BookTitles.Where(x =&gt; x.BookAuthors.Contains(new BookAuthors() {AuthorName = "AAA"})).ToList();
  }
  </code></pre>
  
  <p>How do I query it using Lambda expression and how do i do this using LINQ to Entities?</p>