@inherits Custom.Hybrid.Razor14
@using System.Linq;
@using Dynlist = System.Collections.Generic.IEnumerable<dynamic>;
<!-- unimportant stuff, hidden -->
@{
var persons = AsList(App.Data["Persons"]);
var books = AsList(App.Data["Books"]);
}
@Html.Partial("_header.cshtml")
<div class="row">
<div class="col-lg-7">
<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>
@Html.Partial("../shared/_DefaultInfoSection.cshtml")
</div>
<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 = (book.Authors as Dynlist).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>
<!-- unimportant stuff, hidden -->