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.
⬇️ Result | Source ➡️
-
Douglas Adams
-
Hitchhikers Guide to the Galaxy
-
Terry Pratchett
-
Good Omens
co-authored by
Neil Gaiman
-
The Last Continent
-
Neil Gaiman
-
Good Omens
co-authored by
Terry Pratchett
-
George Akerlof
-
Raphael Müller (not an author)
-
Ed Hardy
@inherits Custom.Hybrid.Razor14
@using System.Linq
@{
// Initial Code
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>