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 218741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:46:47+00:00 2026-05-11T18:46:47+00:00

I am using the Entity Framework. I have a many to many relationship Articles

  • 0

I am using the Entity Framework. I have a many to many relationship Articles <-> Categories. Each article can belong to many categories, and each category can belong to more than one article. I can get the COUNT of articles within each category:

public IEnumerable<MenuCategory> GetMenuCategories()
{
   return (from c in db.CategorySet.Include("Articles")
     orderby c.CategoryName
     select new MenuCategory
     {
        CategoryID = c.CategoryID,
        CategoryName = c.CategoryName,
        CountOfCategory = c.Articles.Count()
      });

}

But I also need to get the total number of articles so that I can calculate what percentage of articles is in each category. Can anyone tell me how to do that by extending the code above and without making a separate call to the database?

Thanks

  • 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-11T18:46:47+00:00Added an answer on May 11, 2026 at 6:46 pm

    The code needs to know the total number of articles in order to do this.

    If you want to avoid a second database call then you can do the calc in the database and return it as an extra column in the result set. There are different ways to acheive this but one would be to use a view and have a SQL function TotalArticles, then your calc is just articleCount / TotalArticles. I don’t know much about the entity-framework so I’m not sure if this is workable.

    If you can’t do the calc in the database then I imagine the second database call is unavoidable although it is just a count of articles so hardly adding much overhead. You could make this call before the main call and then add the value to your results in a similar manner to above, i.e. PercentageOfArticles = c.Articles.Count() / articleCount

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

Sidebar

Related Questions

I'm using Entity Framework 4 and have a one-to-many relationship between a parent and
I am using Entity Framework with my website. To improve performance, I have started
I have a problem with the following Linq query using Entity Framework: from o
So, I am using the Linq entity framework. I have 2 entities: Content and
When using the Entity Framework, does ESQL perform better than Linq to Entities? I'd
I have created a very simple Entity Framework 4.0 model using VS2010 Professional. Ignore
I am using Linq and the Entity Framework. I have a Page Entity, that
I'm building an ASP.NET MVC site using the ADO.NET Entity Framework. I have an
I have a ASP.NET MVC 3 application, using Entity Framework 4 to handle Data
I'm using Entity Framework O/R mapper from Microsoft and using entity classes (generated classes

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.