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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:24:35+00:00 2026-05-12T17:24:35+00:00

I’m attempting to write a linq query which uses several tables of related data

  • 0

I’m attempting to write a linq query which uses several tables of related data and have gotten stuck.

The expected result: I need to return the three most populous metropolitan areas per region by population descending.

tables w/sample data:

MetroAreas — ID, Name
2, Greater New York

Cities — ID, Name, StateID
1293912, New York City, 10

CityPopulations — ID, CityID, CensusYear, Population
20, 1293912, 2008, 123456789
21, 1293912, 2007, 123454321

MetroAreaCities — ID, CityID, MetroAreaID
1, 1293912, 2

States — ID, Name, RegionID
10, New York, 5

Regions — ID, Name
5, Northeast

I start with the metro areas. Join the MetroAreaCities to get city IDs. Join Cities to get state IDs. Join States to get the region ID. Join regions so I can filter with a where. I get stuck when I try to include CityPopulations. I only want the three most populous metro areas for a given region. Doing a simple join on the cityPopulations returns a record per year.

(Here’s what I have so far, this query was written for SubSonic 3):

return from p in GeoMetroArea.All()  
       join q in GeoMetroAreaCity.All() on p.ID equals q.MetroAreaID  
       join r in GeoCity.All() on q.CityID equals r.ID  
       join s in GeoState.All() on r.StateID equals s.ID  
       join t in GeoRegion.All() on s.RegionID equals t.ID  
       where t.ID == regionObjectPassedToMethod.ID  
       select p;  

Can anyone help me with this query or point me in the right direction? Thank you very very much.

  • 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-12T17:24:35+00:00Added an answer on May 12, 2026 at 5:24 pm

    I haven’t compiled it, but this should get you close:

    var regionID = 5;
    
    var year = (from c in GeoCityPopulation.All()
                select c.CensusYear
               ).Max();
    
    var metros =
        // States in Region
        from s in GeoStateAll()
        where s.RegionID == regionID
        // Cities in State
        join c in GeoCity.All() on s.CityID equals c.ID
        // Metro Area for City
        join mc in GeoMetroAreaCity.All() on c.ID equals mc.CityID
        // Population for City
        join cp in GeoCityPopulation.All() on c.ID equals cp.CityID
        where cp.CensusYear = year
        // Group the population values by Metro Area
        group cp.Population by mc.MetroAreaID into g
        select new
        {
            MetroID = g.Key,      // Key = mc.MetroAreaID
            Population = g.Sum()  // g = seq. of Population values
        } into mg
        // Metro for MetroID
        join m in GeoMetroArea.All() on mg.MetroID equals m.ID
        select new { m.Name, mg.Population };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have an autohotkey script which looks up a word in a bilingual dictionary
I have an array which has BIG numbers and small numbers in it. I
I have a text area in my form which accepts all possible characters from
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,

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.