title: Best way to display table data as lists in C# ASP.Net Razor Pages
body: <p>I am working on a project that users can add/edit/delete a book from the database and reserve it. Basically a library database. In my user table(TblUser) I have userId(PK), name, email etc.. In my book table(TblBook) I have bookID(PK), Bookname, author etc. And Finally I have the TblBookStatus which has resID(PK), bookID, userID and status. I did not connect bookID and userID as foreign key due to some project specific reasons. When a user reserves a book, the userID, bookID and status has been added as a row on TblBookStatus table. What I am trying to do is, I want to display the reserved books by a user(TblBookStatus). here is my C# code:</p>
  
  <pre class="lang-cs prettyprint-override"><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Threading.Tasks;
  using LibMan.Models.DB;
  using Microsoft.AspNetCore.Mvc;
  using Microsoft.AspNetCore.Mvc.RazorPages;
  using Microsoft.Data.SqlClient;
  using Microsoft.EntityFrameworkCore;
  
  namespace LibMan.Pages
  {
      public class LibraryModel : PageModel
      {
          private readonly LibMan.Models.DB.LibManContext _context;
          public string Message { get; set; }
          public LibraryModel(LibMan.Models.DB.LibManContext context)
          {
              _context = context;
          }
          public IList&lt;TblBookStatus&gt; TblBookStatus { get; set; }
          public IList&lt;TblBook&gt; TblBook { get; set; }
          public IList&lt;TblBook&gt; DisplayBook { get; set; }
  
          public async Task OnGetAsync()
          {
              TblBookStatus = await _context.TblBookStatus.ToListAsync();
              TblBook = await _context.TblBook.ToListAsync();
              foreach(var item in TblBook)
              {
                  foreach(var item2 in TblBookStatus)
                  {
                      if(item.BookId == item2.BookId)
                      {
                          DisplayBook.Add(item);
                      }
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>I tried to turn both tables into lists and check for the intersecting book ID's and make a new list to display them, but I get the error : <strong>NullReferenceException: Object reference not set to an instance of an object.</strong> on the line <code>DisplayBook.Add(item);</code></p>
  
  <p>I have seen people writing SQL commands, but I am not sure If I should do it.
  Here is the HTML side</p>
  
  <pre class="lang-html prettyprint-override"><code>@page
  @model LibMan.Pages.LibraryModel
  @{
      ViewData["Title"] = "Library";
      Layout = "~/Pages/Shared/_postLoginLayout.cshtml";
  }
  
  &lt;h1&gt;Library&lt;/h1&gt;
  
  &lt;p&gt;
      &lt;a asp-page="Create"&gt;Add a new Book&lt;/a&gt;
  &lt;/p&gt;
  &lt;table class="table"&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th&gt;
                  @Html.DisplayNameFor(model =&gt; model.TblBook[0].Title)
              &lt;/th&gt;
              &lt;th&gt;
                  @Html.DisplayNameFor(model =&gt; model.TblBook[0].Author)
              &lt;/th&gt;
              &lt;th&gt;
                  @Html.DisplayNameFor(model =&gt; model.TblBook[0].Translator)
              &lt;/th&gt;
              &lt;th&gt;
                  @Html.DisplayNameFor(model =&gt; model.TblBook[0].Publisher)
              &lt;/th&gt;
              &lt;th&gt;
                  @Html.DisplayNameFor(model =&gt; model.TblBook[0].Description)
              &lt;/th&gt;
              &lt;th&gt;
                  @Html.DisplayNameFor(model =&gt; model.TblBook[0].Category)
              &lt;/th&gt;
              &lt;th&gt;
                  @Html.DisplayNameFor(model =&gt; model.TblBook[0].Status)
              &lt;/th&gt;
              &lt;th&gt;
                  @Html.DisplayNameFor(model =&gt; model.TblBook[0].Cover)
              &lt;/th&gt;
              &lt;th&gt;&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
          @foreach (var item in Model.DisplayBook)
          {
              &lt;tr&gt;
                  &lt;td&gt;
                      @Html.DisplayFor(modelItem =&gt; item.Title)
                  &lt;/td&gt;
                  &lt;td&gt;
                      @Html.DisplayFor(modelItem =&gt; item.Author)
                  &lt;/td&gt;
                  &lt;td&gt;
                      @Html.DisplayFor(modelItem =&gt; item.Translator)
                  &lt;/td&gt;
                  &lt;td&gt;
                      @Html.DisplayFor(modelItem =&gt; item.Publisher)
                  &lt;/td&gt;
                  &lt;td&gt;
                      @Html.DisplayFor(modelItem =&gt; item.Description)
                  &lt;/td&gt;
                  &lt;td&gt;
                      @Html.DisplayFor(modelItem =&gt; item.Category)
                  &lt;/td&gt;
                  &lt;td&gt;
                      @Html.DisplayFor(modelItem =&gt; item.Status)
                  &lt;/td&gt;
                  &lt;td&gt;
                      @Html.DisplayFor(modelItem =&gt; item.Cover)
                  &lt;/td&gt;
                  &lt;td&gt;
                      &lt;a asp-page="./GetBook" asp-route-id="@item.BookId"&gt;Get Book&lt;/a&gt; |
                      &lt;a asp-page="./Edit" asp-route-id="@item.BookId"&gt;Edit&lt;/a&gt; |
                      &lt;a asp-page="./Details" asp-route-id="@item.BookId"&gt;Details&lt;/a&gt; |
                      &lt;a asp-page="./Delete" asp-route-id="@item.BookId"&gt;Delete&lt;/a&gt;
                  &lt;/td&gt;
              &lt;/tr&gt;
          }
      &lt;/tbody&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>Thanks for the help!</p>