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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:06:50+00:00 2026-06-02T20:06:50+00:00

I have two tables in my database, Building and Town. They look like this:

  • 0

I have two tables in my database, Building and Town. They look like this:

Building:

buildingid
buildingname

Town:

id
userid
buildingid

In Town there is one entry for each building a user has.

What i want is to populate a GridView for a user with a given userid. This GridView should include the buildingname and the number of buildings.
Building. I have tried this:

var buildings = (from Town in dc.Towns
                         join Building in dc.Buildings
                             on Town.buildingid equals Building.buildingid
                         select Building.buildingname);
        gvBuildings.DataSource = buildings;
        gvBuildings.DataBind();

But I don’t know how to get the numbers for each building.


I have now been working on this for a while and a couple of your answers work. I have used this code:

var buildings = dc.Towns
            .Where(t => t.userid == userid)
            .GroupJoin(dc.Buildings,
                       t => t.buildingid,
                       b => b.buildingid,
                       (Town, Buildings) => new
                                                {
                                                    BuildningName = Buildings.First().buildingname,
                                                    Count = Buildings.Count()
                                                });
        gvBuildings.DataSource = buildings.ToList();
        gvBuildings.DataBind();

When i run this code my GridView ends up looking like this:
GridView

I need the buildings to be shown in groups, grouped by the buildingname. I have tried all of the suggestions but i cant get it to work.

  • 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-02T20:06:51+00:00Added an answer on June 2, 2026 at 8:06 pm

    Try grouping:

    var buildings = dc.Towns
                      .Where(t => t.UserId == userId)
                      .GroupJoin(dc.Buildings,
                                 t => t.BuildingId,
                                 b => b.BuildingId,
                                 (town, buildings) => new
                                 {
                                     BuildingName = buildings.First().BuildingName,
                                     Count = buildings.Count
                                 });
    

    Keep in mind that when binding to a control you must supply a collection of type (or implementing) IList. This can be accomplished by calling ToList() on the buildings collection:

    gvBuildings.DataSource = buildings.ToList();
    gvBuildings.DataBind();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables in my database: Town: userid, buildingid Building: buildingid, buildingname What
I have two tables in my database that look like that: Customer: C_ID city
I have two tables in my MySQL database, one is a library of all
I have a database containing two tables: event and session (one event has multiple
I have two database tables, 1st is the TABLE_GLOBAL_PRODUCTS where all products information are
I have two database tables with the following structure: actions: action_id int(11) primary key
I have two tables in my database schema that represent an entity having a
I have two tables in my database. Comments CommentsID MembersID CommentsBy CommentsDesc CommentsActive CommentsDateEntered
I have two tables in my database: user and media_contact . The media_contact table
I have two tables in my database table1, table2. I have a join query

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.