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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:21:26+00:00 2026-06-08T06:21:26+00:00

I have two 2 tables in my database Table: Foo Table: Bar —————– ———————

  • 0

I have two 2 tables in my database

Table: Foo                 Table: Bar
-----------------          ---------------------
|FooID     | Int|          |BarID     | Int    |
|Number    | Int|          |FooID     | Int    |
-----------------          |Name      | String |
                           |Value     | Int    |
                           ---------------------

With data                  with data
|FooID | Number |          |BarID |FoodID |Name    |Value |
|1     | 1      |          |1     |1      |apple   |100   |
|2     | 2      |          |2     |1      |orange  |110   |
                           |3     |2      |apple   |200   |
                           |4     |2      |orange  |40    |

These are the related models

class Foo
{
    public int FooID { get; set; }
    public int Number { get; set;}

    public virtual ICollection<Bar> Bars { get; set; }
}

class Bar
{
    public int BarID { get; set; }
    public int FooID { get; set; }
    public string Name { get; set;}
    public int Value { get; set;}
}

I can easly display this in a table like format by doing the follwing in the view

<table>
    @foreach(var f in Model)
    {
        <tr>
            foreach(var b in f.Bar)
            {
                <td>@f.Number</td>
                <td>@b.Name</td>
                <td>@b.Value</td>
            }
        </tr>
    }
</table>

Which outputs

-------------------
|1   |apple    |100|
-------------------
|1   |orange   |110|
-------------------
|2   |apple    |200|
-------------------
|2   |orange   | 40|
-------------------

What I’d really to see for the output is the following.

-------------------------
|         |  1      |  2 |
-------------------------
|apple    |100      |200 |
-------------------------
|orange   |200      | 40 |
-------------------------

Can someone please point me in the right direction???

  • 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-08T06:21:28+00:00Added an answer on June 8, 2026 at 6:21 am

    Maybe there are more elegant solutions exist, but here is how I would do it.

    You need one loop to generate the header rows from Foo.Number then you need a second loop where you select all the Bars and group them by their Name. From these groups you can generate the data rows.

    Now you only need a thrid loop which goes through the Bar.Values and builds the table row.

    So the above described “algorithm” in code:

    <table>
        <tr>
            <td>
                &nbsp;
            </td>
            @foreach (var f in Model.OrderBy(f => f.FooID))
            {
                <td>@f.Number
                </td>
            }
        </tr>
        @foreach (var group in Model.SelectMany(f => f.Bars).GroupBy(b => b.Name))
        {
            <tr>
                <td>@group.Key
                </td>
                @foreach (var b in group.OrderBy(b => b.FooID))
                {
                    <td>@b.Value
                    </td>
                }
            </tr>
        }
    </table>
    

    Note: I’ve added the OrderBy(b => b.FooID) to make sure that the Bar values correctly aligned with the Foo.Number headers.

    And this how the result looks like:

    enter image description here

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

Sidebar

Related Questions

I have a database with two tables - let's call them Foo and Bar.
I have two classes Foo and Bar . The tables in the database look
I have the following two tables in my mysql database. Table Name: groups id
Suppose I have a database table with two fields, foo and bar. Neither of
I have two tables in my database as following: Employee Table: Username, Name, Job,
I have two tables in my database Table:Documents Id (int), DocName (nvarchar) ---------------------------- Table:AccessLogs
I have two tables in my database: user and media_contact . The media_contact table
I have a database framework where I have two tables. The first table has
In a MySQL database I have two tables linked in a join. One table
I have a database two tables and a linking table that I need a

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.