title: MVC ASP 3 table join then display results
body: <p>For a project, I'm displaying books for adding to basket. I need to join 3 tables in order to show the formats available: I do: Books to BookFormats on bookID, then BookFormats to Format on formatID. This is a prelaid out database which i cannot change. I wrote a LINQ  query to display them all and made a view model with the properties of all 3.</p>
  
  <p><strong>The linq query:</strong></p>
  
  <pre><code>        IEnumerable&lt;BookAndFormatViewModel&gt; IEBooksRepository.GetAllBooksWithFormats()
      {
          var query =
              from book in eBooks.Books
              join format in eBooks.BookFormats on book.BookID equals format.BookID
              join formatName in eBooks.Formats on format.FormatID equals formatName.FormatID
              select new BookAndFormatViewModel();
          return query.ToList();
  </code></pre>
  
  <p><strong>The view model for the three:</strong></p>
  
  <pre><code>    public int BookID { get; set; }
      public string ReferenceNumber { get; set; }
      public string Title { get; set; }
      public decimal Price { get; set; }
      public int FormatID { get; set; }
      public string FormatType { get; set; }
  </code></pre>
  
  <p><strong>The controller code:</strong></p>
  
  <pre><code>        IEnumerable&lt;BookModel&gt; books = repository.GetAllBooks();
          IEnumerable&lt;BookAndFormatViewModel&gt; booksWithFormat = repository.GetAllBooksWithFormats();
  
          if (books.Count() == 0)
          {
              return PartialView("NoBooks");
  
          }
          return PartialView("Books", booksWithFormat);   
  </code></pre>
  
  <p>When I run it it displays empty data just 0's and null fields.</p>