嗨,我有两张桌子,BlogPost和BlogComments
我想得到所有状态为“已发布”的BlogPosts。并获取每个状态为"published“的文章的所有BlogComments。
我正在使用类似这样的东西来获取博客帖子:
var BlogPosts = (from p in db.BlogPosts where p.State == State.Published select p).ToArray();但由于与BlogComments的关系,它自动拥有所有的BlogComments (包括已发布和未发布)。
如何才能获得每个博客帖子的“已发布”评论(即已批准的评论)?
谢谢
发布于 2010-03-10 21:00:05
尝试选择一个新的BlogPostViewModel,类似于BlogPost,但使用的是IEnumerable<BlogComment>,只包含博客帖子数据和一组已发布的评论。
select new BlogPostViewModel {
Title = p.Title,
Body = p.Body,
Comments = p.Comments.Where( c => c.Published );
});其中,BlogPostViewModel是:
public class BlogPostViewModel
{
public string Title { get; set; }
public string Body { get; set; }
public IEnumerable<BlogComment> Comments { get; set; }
}发布于 2010-03-10 21:00:17
var BlogPosts = from p in db.BlogPosts where p.State == State.Published select new {
Post = p,
Comments = (from c in db.BlogComments where c.State == State.Published && c.Post = p select c)
};发布于 2010-03-10 21:00:37
var publishedComments = db.BlobPosts.Where(p => p.State == State.Published)
.SelectMany(p => p.Comments)
.Where(c => c.State == State.Published);很抱歉没有使用查询语法。不过,希望这篇文章能让你上路。
https://stackoverflow.com/questions/2416974
复制相似问题