【发布时间】:2009-08-12 06:20:42
【问题描述】:
在实体框架(和 LINQ-to-Entities)中有两个类,如 Blog 和 Post,如何获取按日期排序的博客。我是通过这种方式获取带有帖子的博客:
from blog in db.BlogSet.Include("Posts") select blog
现在我不得不这样做:
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)
}
这是非常复杂和丑陋的。我创建一个 BlogPosts 类的原因是,由于我必须将两个变量 Blog 和 Posts 传递给 MVC,我需要一个视图模型。
我什至很想试试这个技巧:
from blog in db.BlogSet
select new Blog(blog) {
Posts = blog.Posts.OrderByDescending(p => p.PublicationTime)
}
但是正确的方法是什么? Entity Framework 不适合 MVC 吗?
【问题讨论】:
标签: asp.net-mvc entity-framework linq-to-entities