Entity Framework Fetch count of child entities as a property

c# entity-framework entity-framework-6 sql

Question

I have two entities that showPost , andComments .

public class Post
{
    public int Id { get; set; }
    public ICollection<Comment> Comments { get; set; }

    //other properties 


    //Unmapped property
    public int NumberOfComments { get; set; }

}

public class Comment
{
    public int Id { get; set; }
    public Post Post { get; set; }
    public int PostId { get; set; }

    // other properties
}

I now want theNumberOfComments attribute that will be filled with the post's real comment count.

  • The attempt to represent the query results as a collection of posts didn't succeed.

  • Attempts to join the table and then group by post id were unsuccessful.

I cannot justreturn p.Comments.Count; because I'm not adding the comments in the query, as the property definition. I don't need to keep the whole collection in memory; only the number of comments.

1
0
11/21/2016 5:59:49 PM

Popular Answer

ZZZ_tmp
1
1/22/2018 11:23:38 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow