#7 Get Authors of Books (sub-items) to show and for sorting
Accessing Authors, a List inside of 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"]);
}
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
- Hitchhikers Guide to the Galaxy
by Douglas Adams
- Good Omens
by Neil Gaiman, Terry Pratchett
- Phishing for Phools
by George Akerlof
- 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
- Good Omens
by Neil Gaiman,Terry Pratchett (2 author)
- Hitchhikers Guide to the Galaxy
by Douglas Adams (1 author)
- Phishing for Phools
by George Akerlof (1 author)
- 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
@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 })