Skip to main content
Home  › ... Razor

LINQ Tutorials

Tutorial HomeLINQ
#10 Go backwards - find Books pointing to Authors with Parents(...)

Find Parents of Authors - Things that point to Authors

In this example, we'll start with the authors list. This is probably not ideal - as some people are not authors, but it's a good learning example. To find the books we have to navigate through Parents(...) because in our data-model, the books reference authors, not the authors to books.

Initial Code

The following code runs at the beginning and creates some variables/services used in the following samples.

@{
  var persons = AsList(App.Data["Persons"]);
  var books = AsList(App.Data["Books"]);
}

Output

  1. Douglas Adams
    • Hitchhikers Guide to the Galaxy
  2. Terry Pratchett
    • Good Omens co-authored by Neil Gaiman
    • The Last Continent
  3. Neil Gaiman
    • Good Omens co-authored by Terry Pratchett
  4. George Akerlof
    • Phishing for Phools
  5. Raphael Müller (not an author)
  6. Ed Hardy
<ol>
  @foreach (var author in persons) {
    // this line would work, if Books only had people in the Authors. 
    // but it doesn't, there are also illustrators, which is why we use the second example instead
    var peoplesBooks = author.Parents("Books");
    var authorsBooks = author.Parents("Books", "Authors");

    <li>
      @author.FirstName @author.LastName
      <ul>
        @foreach (var book in authorsBooks) {
          var coAuthors = AsList(book.Authors as object).Where(a => a != author);
          <li>
            <strong>@book.Title</strong> 
            @if (coAuthors.Any()) {
              <span>co-authored by 
                @string.Join(",", coAuthors.Select(a => a.FirstName + " " + a.LastName))
              </span>
            }
          </li>
        }
      </ul>
    </li>
  }
</ol>
#10 Go backwards - find Books pointing to Authors with Parents(...)

Source Code of this file

Below you'll see the source code of the file. Note that we're just showing the main part, and hiding some parts of the file which are not relevant for understanding the essentials. Click to expand the code

@inherits Custom.Hybrid.Razor14
@using ToSic.Razor.Blade;
@using System.Linq;
<!-- unimportant stuff, hidden -->


<div @Sys.PageParts.InfoWrapper()>
  @Html.Partial("../shared/DefaultInfoSection.cshtml")
  <div @Sys.PageParts.InfoIntro()>
    <h3>Find Parents of Authors - Things that point to Authors</h3>
    <p>In this example, we'll start with the authors list. This is probably not ideal - as some people are not authors, but it's a good learning example. To find the books we have to navigate through <code>Parents(...)</code> because in our data-model, the books reference authors, not the authors to books.</p>
  </div>
</div>

  @{
    var persons = AsList(App.Data["Persons"]);
    var books = AsList(App.Data["Books"]);
  }


  <ol>
    @foreach (var author in persons) {
      // this line would work, if Books only had people in the Authors. 
      // but it doesn't, there are also illustrators, which is why we use the second example instead
      var peoplesBooks = author.Parents("Books");
      var authorsBooks = author.Parents("Books", "Authors");

      <li>
        @author.FirstName @author.LastName
        <ul>
          @foreach (var book in authorsBooks) {
            var coAuthors = AsList(book.Authors as object).Where(a => a != author);
            <li>
              <strong>@book.Title</strong> 
              @if (coAuthors.Any()) {
                <span>co-authored by 
                  @string.Join(",", coAuthors.Select(a => a.FirstName + " " + a.LastName))
                </span>
              }
            </li>
          }
        </ul>
      </li>
    }
  </ol>



@* Footer *@
@Html.Partial("../Shared/Layout/FooterWithSource.cshtml", new { Sys = Sys })