So I am working on a self made forum (ASP.net MVC4) and I am currently displaying all the forums, in the controller I do this easy query:
return View(db.Forums.ToList());
But now I want to be able to also include the top post. (a Forum entity has a collection of Post, Post has a Date, I want to order by that and then do a take(1)).
I try something like:
return View(db.Forums.Include(z=>z.Posts.OrderBy(x=>x.Date).Take(1)).ToList());
Then I get the error:
The Include path expression must refer to a navigation property defined on the type. Use dotted paths for reference navigation properties and the Select operator for collection navigation properties. Parameter name: path
PS: here is the current view
@model IEnumerable<MyProject.Data.Forum>
@{
ViewBag.Title = "Forum Index";
}
<h2>Forums</h2>
<table class="Forum">
<tbody>
<tr>Main forums</tr>
@foreach (var item in Model)
{
<tr class="ForumItem">
<td><a href="Index?id=@item.Id">X</a></td>
<td><p><a href="Index?id=@item.Id">@item.Name</a></p><span>@item.Description</span></td>
<td>@item.PostCount</td>
@foreach(var post in item.Posts)
{
<td>@post.Title</td>
}
</tr>
}
</tbody>
</table>
Anyone knows a solution for this?