Having two classes like Blog and Post, in Entity Framework (and LINQ-to-Entities), how can you get the blogs with the posts sorted by date. I was getting the blogs with the posts this way:
from blog in db.BlogSet.Include("Posts") select blog
and now I'm forced to do this:
public class BlogAndPosts {
public Blog Blog { get; set; }
public IEnumerable<Post> Posts { get; set; }
}
from blog in db.BlogSet
select new BlogAndPosts () {
Blog = blog,
Posts = blog.Posts.OrderByDescending(p => p.PublicationTime)
}
which is very convoluted and ugly. The reason why I'm creating a BlogPosts class is that now, since I have to pass two variables, Blog and Posts, to MVC, I need a view model.
I'm even tempted to try this hack:
from blog in db.BlogSet
select new Blog(blog) {
Posts = blog.Posts.OrderByDescending(p => p.PublicationTime)
}
but what's the correct way to do it? Is Entity Framework not the way to go with MVC?