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
co-authored by
Douglas Adams
-
Terry Pratchett
-
Good Omens
co-authored by
Neil Gaiman,Terry Pratchett
-
The Last Continent
co-authored by
Terry Pratchett
-
Neil Gaiman
-
Good Omens
co-authored by
Neil Gaiman,Terry Pratchett
-
George Akerlof
-
Phishing for Phools
co-authored by
George Akerlof
-
Raphael Müller (not an author)
-
Ed Hardy
@inherits Custom.Hybrid.RazorTyped
@using System.Linq
@{
// Initial Code
var persons = AsItems(App.Data.GetStream("Persons"));
var books = AsItems(App.Data.GetStream("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(type: "Books");
var authorsBooks = author.Parents(type: "Books", field: "Authors");
<li>
@author.String("FirstName") @author.String("LastName")
<ul>
@foreach (var book in authorsBooks) {
var coAuthors = AsItems(book.Children("Authors"))
.Where(a => a != author);
<li>
<strong>@book.Get("Title")</strong>
@if (coAuthors.Any()) {
<span>co-authored by
@string.Join(",", coAuthors
.Select(a => a.String("FirstName") + " " + a.String("LastName")))
</span>
}
</li>
}
</ul>
</li>
}
</ol>