Home > database >  C# - Table - Merging repetitive data into one line (applying for just one field)
C# - Table - Merging repetitive data into one line (applying for just one field)

Time:03-29

I am sorry if this question is already answered, but I am trying to change the actual table result on front end.

from this (which is the same structure in the query result):

 ---- -------- ------------------ ------------------ --------- 
| id | name   | in               | out              | country |
 ---- -------- ------------------ ------------------ --------- 
| 23 | Carlos | 23/03/2022 08:00 | 23/03/2022 17:00 | France  |
 ---- -------- ------------------ ------------------ --------- 
| 23 | Carlos | 24/03/2022 08:10 | 24/03/2022 17:10 | France  |
 ---- -------- ------------------ ------------------ --------- 
|  4 | Amanda | 23/03/2022 08:00 | 23/03/2022 17:00 | Italy   |
 ---- -------- ------------------ ------------------ --------- 

to this:

 ---- -------- ------------------ ------------------ --------- 
| id | name   | in               | out              | country |
 ---- -------- ------------------ ------------------ --------- 
|    |        | 23/03/2022 08:00 | 23/03/2022 17:00 | France  |
| 23 | Carlos  ------------------ ------------------ --------- 
|    |        | 24/03/2022 08:10 | 24/03/2022 17:10 | France  |
 ---- -------- ------------------ ------------------ --------- 
|  4 | Amanda | 23/03/2022 08:00 | 23/03/2022 17:00 | Italy   |
 ---- -------- ------------------ ------------------ --------- 

I tried to use front end to do that but it merge all line, so taking the example, carlos will be displayed in just one line, getting just the first element of each field, which is not the right because will omit values.

So I am guessing that this should be done in the query.. but I am not sure how that helps me though

Actual code in the view:

<table>
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>Name</th>
                        <th>In</th>
                        <th>Out</th>
                        <th>Country</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach(var r in Model.Reports.OrderBy(x => x.Id))
                    {
                        <tr>
                            <td>@r.UserId</td>
                            <td>@r.Name</td>
                            <td>@r.In</td>
                            <td>@r.Out</td>
                            <td>@r.Country</td>
                        </tr>
                    }
                </tbody>
            </table>

CodePudding user response:

You can try to count the number of repetitive data,and then use rowspan,here is a working demo:

<table border="1">
    <thead>
        <tr>
            <th>ID</th>
            <th>Name</th>
            <th>In</th>
            <th>Out</th>
            <th>Country</th>
        </tr>
    </thead>
    <tbody>
        @{ var row = 1;}
        @foreach (var r in Model.Reports.OrderBy(x => x.Id))
        {
            if (Model.Reports.Where(p => p.UserId == r.UserId).ToList().Count > 1)
            {
                if (row == 1)
                {
                    <tr>
                        <td [email protected](p => p.UserId == r.UserId).ToList().Count>@r.UserId</td>
                        <td [email protected](p => p.UserId == r.UserId).ToList().Count>@r.Name</td>
                        <td>@r.In</td>
                        <td>@r.Out</td>
                        <td>@r.Country</td>
                    </tr>
                    row  ;
                }
                else
                {
                    <tr>
                        <td>@r.In</td>
                        <td>@r.Out</td>
                        <td>@r.Country</td>
                    </tr>
                    if (row == Model.Reports.Where(p => p.UserId == r.UserId).ToList().Count)
                    {
                        row = 1;
                    }
                    else {
                        row  ;
                    }

                }

            }
            else {
                <tr>
                    <td>@r.UserId</td>
                    <td>@r.Name</td>
                    <td>@r.In</td>
                    <td>@r.Out</td>
                    <td>@r.Country</td>
                </tr>
            }
            
        }
    </tbody>
</table>

result:

enter image description here

  • Related