Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6114561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:59:20+00:00 2026-05-23T14:59:20+00:00

Supose the model as below: class public Post { public int Id {get; set;}

  • 0

Supose the model as below:

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

in the Posts/Index Page, I want to show a list of Post, with the Count of comments of each post (not total number of comments of all posts).

1: If I use

context.Posts.Include("Comments")

it will load the whole entity of all related commments , in fact I only need the Count of Comments.

2: If I get the count of each post one by one:

var commentCount = context.Entry(post)
                      .Collection(p => p.Comments)
                      .Query()
                      .Count();

that is a N+1 problem.

Any one knows the right way?

Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T14:59:21+00:00Added an answer on May 23, 2026 at 2:59 pm

    Do you need this for your presentation layer / view model? In such case create specialized ViewModel

    public class PostListView 
    {
        public Post Post { get; set; }
        public int CommentsCount { get; set; }
    }
    

    And use query with projection:

    var data = context.Posts
                      .Select(p => new PostListView
                          {
                              Post = p,
                              CommentsCount = p.Comments.Count()
                          });
    

    And you are done. If you need it you can flatten your PostListView so that it contains Post‘s properties instead of Post entity.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have class Foo(db.Model): bar = db.ReferenceProperty(Bar) foo = Foo.all().get() Is there a
Suppose the following model public class Product { private Integer requiredQuantity; private Integer allowedQuantity;
Supose there is table UserProfile: class UserProfile(models.Model): name = models.CharField(max_length=30, db_index=True) # db_index 1
Suppose I have an Comment model that belongs_to a Post model. I want to
Suppose my model is this: class Ego(models.Model): event = models.ForeignKey(Event) user = models.ForeignKey(User) As
Suppose the .NET Component Model. The Container class has GetService(Type service); But I'm asking
Suppose I have the following models: class User(models.Model): pass class A(models.Model): user = models.ForeignKey(User)
Suppose I have an Orders table in my database and a corresponding model class
Author post-edit: Chosen solution (Original question remains below this box) SUMMARY: You SHOULD NOT
Suppose my model looks like this: class Farm(models.Model): name = ... class Tree(models.Model): farm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.