Skip to main content
Home  › ... Razor

LINQ Tutorials

Tutorial HomeLINQ
#7 Get Authors of Books (sub-items) to show and for sorting

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"]);
}

Basic Use of List book.Authors

This example shows the books, and we want to LINQ on the Authors. We must tell the compiler it's a list, so that LINQ can use Select(...).

Output

  1. Hitchhikers Guide to the Galaxy by Douglas Adams
  2. Good Omens by Neil Gaiman, Terry Pratchett
  3. Phishing for Phools by George Akerlof
  4. The Last Continent by Terry Pratchett
<ol>
  @foreach (var book in books) {
    // To work with a list coming off a dynamic object, we have to tell the compiler it's a dynamic list
    var authors = AsList(book.Authors as object)
      .Select(a => a.FirstName + " " + a.LastName);

    <li><strong>@book.Title</strong>
      by @string.Join(", ", authors.OrderBy(ln => ln)) 
    </li>
  }
</ol>

OrderBy and OrderByDescending with Amount of Authors

This example shows Z-A ordering, where we count the authors to sort.

Output

  1. Good Omens by Neil Gaiman,Terry Pratchett (2 author)
  2. Hitchhikers Guide to the Galaxy by Douglas Adams (1 author)
  3. Phishing for Phools by George Akerlof (1 author)
  4. The Last Continent by Terry Pratchett (1 author)
<ol>
  @foreach (var book in books.OrderByDescending(p => p.Authors.Count)) {
    var authors = AsList(book.Authors as object).Select(a => a.FirstName + " " + a.LastName);
    <li><strong>@book.Title</strong> 
      by @string.Join(",", authors.OrderBy(ln => ln)) (@book.Authors.Count author) 
    </li>
  }
</ol>
#7 Get Authors of Books (sub-items) to show and for sorting

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()>
    <h2>Accessing Authors, a List inside of Books</h2>
  </div>
</div>

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



<h3>Basic Use of List <code>book.Authors</code></h3>
<p>This example shows the books, and we want to LINQ on the Authors. We must tell the compiler it's a list, so that LINQ can use <code>Select(...)</code>.</p>

  <ol>
    @foreach (var book in books) {
      // To work with a list coming off a dynamic object, we have to tell the compiler it's a dynamic list
      var authors = AsList(book.Authors as object)
        .Select(a => a.FirstName + " " + a.LastName);

      <li><strong>@book.Title</strong>
        by @string.Join(", ", authors.OrderBy(ln => ln)) 
      </li>
    }
  </ol>



<h3>OrderBy and OrderByDescending with Amount of Authors</h3>
<p>This example shows Z-A ordering, where we count the authors to sort.</p>

  <ol>
    @foreach (var book in books.OrderByDescending(p => p.Authors.Count)) {
      var authors = AsList(book.Authors as object).Select(a => a.FirstName + " " + a.LastName);
      <li><strong>@book.Title</strong> 
        by @string.Join(",", authors.OrderBy(ln => ln)) (@book.Authors.Count author) 
      </li>
    }
  </ol>



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