Home > Back-end >  Pivoting a table in Blazor
Pivoting a table in Blazor

Time:09-14

I have a SQL Server table which looks like this:

Month | Percentage
1         5
2         6
3         8
4         10

I would like to show this table in a Blazor view but pivoted such that it looks like :

Month      1  2  3  4
Percentage 5  6  8  10

I wanted to know if something like this is possible as I have never worked with pivots in c# before

CodePudding user response:

This is a website that shows how to pivot a table, if that is what you need :) https://www.codeproject.com/Articles/22008/C-Pivot-Table

CodePudding user response:

Like Henk said in the comments, you can use 2 foreach loops

<table >
    <tr>
        <td>
            Month
        </td>
        @foreach (var val in YourSqlTableModelList)
        {
            <td>@val.Month</td>
        }
    </tr>
    <tr>
        <td>
            Percentage
        </td>
        @foreach (var val in YourSqlTableModelList)
        {
            <td>@val.Percentage</td>
        }
    </tr>
</table>

@code{
        protected List<YourSqlTableModel> YourSqlTableModelList { get; set;}

        public class YourSqlTableModel
        {
            public string Month { get; set;}
            public string Percentage { get; set;}
        }
        
        //call the method to get the values from the sql server table
    }
  • Related