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

  • SEARCH
  • Home
  • 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 7999489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:31:16+00:00 2026-06-04T15:31:16+00:00

Im trying to get a count on how many people live in a certain

  • 0

Im trying to get a count on how many people live in a certain city. I have a database with people in it, and that table has a foreign key linking a certain person to a city, which is another table.

example:

City z: 5 people

City y: 10 people

City x: 4 people

im able to get these results back, but i just dont like the way i’m doing it, as i am calling the database x amount of times.

 public List<int> getStuff(List<int> listOfCityIDs )
    {
       var returnList = new List<int>();
       foreach (int z in listOfCityIDs)
        {
             returnList.Add((from x in conn.people
                             where x.city == z
                             select x).Count());
        }
        return returnList;
    }

im pretty sure there’s a better/more efficient way of doing it with some LINQ, but i can’t seem to find how.

any ideas?

kind regards,
Jane

  • 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-06-04T15:31:18+00:00Added an answer on June 4, 2026 at 3:31 pm

    This will translate into SQL statements nicely.

    conn.people.GroupBy(p => p.city).Select(p => new { City = p.Key, Count = p.Count()});
    

    This will get them all. If you want based for certain cities, try

    conn.people.Where(p => listOfCityIDs.Any(c => c == p.city))
      .GroupBy(p => p.city).Select(p => new { City = p.Key, Count = p.Count()});
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to get a simple COUNT from a table that takes a couple of
I'm trying to get a count of users who have paid for a service
I am trying to get a timer to count two times but I have
I have a CakePHP application where I am trying to get a count of
I'm trying to get a count of records matching certain criteria within a subset
I'm trying to count how many essays have been graded so I know how
Am trying to get the count of how many check boxes are selected and
Given a list of records, I'm trying to get a count of how many
I am trying to get a count of messages in my MSMQ. I have
I'm trying to get Items that has at least 1 Chart, Items and Charts

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.