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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:55:29+00:00 2026-05-31T22:55:29+00:00

I have 2 tables, 1 with countries, 1 with states. The states table has

  • 0

I have 2 tables, 1 with countries, 1 with states.
The states table has a column with Population.

I’m using entities and I have created a List of states for the countries

public class TblCountries
{
//Entities for my table country
...
public List<tblStates> States { get; set; }
}

So now I can for example List all the states that belong to a country.

Now what I want to do is count the population, so I can show the population that of an entire country.

I tried using in my view

@foreach (var item in Model.Countries) {
 @Html.DisplayFor(modelItem => item.States.Count<population>)
}

But this doesn’t work, anyone know how to do this?

Thanks in advanced!

  • 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-31T22:55:30+00:00Added an answer on May 31, 2026 at 10:55 pm

    You need to use the Linq Sum Function

    I haven’t checked this code, but it would be something like this:

    @foreach (var item in Model.Countries) {
     var states = item.states;
     states.Select(state => state.population).Sum()
    }
    

    Dmitriy’s opinion about keeping the logic in the middle-tier is sound advice; however, those lines are blurring with .NET MVC and the view model pattern (which causes much grumbling among the purists out there).

    My recommendation is that if you can isolate your logic in a class behind your controller, then do so. If that is not possible for a given view then feel free to add logic to your view. That, after all, is what the syntax is for.

    An example of a time that you may need to do this is if you have a generic view model delivered to many views but need a few specific logic tweaks in certain views.

    Good luck!

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

Sidebar

Related Questions

I have 4 tables namely, countries, states, cities, areas, apart from countries table the
I have two tables, one called countries and one called country. Countries has both
I have a table that is a list of countries, with two fields, ID
I have two tables (country & ducks) where the country table has every country
I have following mysql database tables: cities states countries with members samajs (a group
i have a countries, states, cities tables , and I need 3 dropdown menus
I have four tables, namely countries,states,cities,areas which will be the best feasible solution for
Well here's my problem I have three tables; regions, countries, states. Countries can be
A have a hash-table with countries in it like this: @countries = {'United States'
I have a table that represents a list of countries. I have another table

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.