i’m trying to learn Entity Framework and I need some help to build my query.
I have a Post class like this :
public class Post
{
public int PostID {get;set;}
public string Title {get;set;}
public string Content {get;set;}
public PostStatus Status {get;set;}
public IList<Comment> Comments {get; set;}
}
I also have a Comment class :
public class Comment
{
public int CommentID {get;set;}
public string Content {get;set;}
public CommentStatus Status {get;set;}
}
What I want is to retrieve all Article with Status == PostStatus.Published, including Comment with Status == CommentStatus.Published.
As you understand, I want to display in a blog all published article with their published comment.
I retrieve all published article with all comments, but I only want to get their published comment, not pending ones.
var result = from art in context.Posts.Include("Comments")
where art.Status == PostStatus.Published
select art;
Thanks for your help
After some research and using your answers, I found that the good way to go is :