Simulate a Master / Detail report with foreach

3

I have a table as follows:

public class Producao
{
    string unidade;
    string profissional;
    string procedimento;
}

I can get all the records stored in the table and throw them in a listaProducao list. And list them by foreach as follows:

<table>
<tr>
    <td>Unidade</td>
    <td>Profissional</td>
    <td>Procedimento</td>
</tr>
    @foreach (var item in Model)
    {
        <tr>
            <td>@item.unidade</td>
            <td>@item.profissional</td>
            <td>@item.procedimento</td>
        </tr>
    }
</table>

The result is similar to the one shown below:

unidade - profissional - procedimento
   1           1              1
   1           2              1
   1           2              2
   1           3              1
   1           3              3

What I want is to generate a result like this:

UNIDADE 1
--------------------
PROFISSIONAL 1
--------------------
procedimento 1
--------------------
PROFISSIONAL 2
--------------------
procedimento 1
procedimento 2
--------------------
PROFISSIONAL 3
--------------------
procedimento 1
procedimento 3

Notice that the result is similar to a Master / Detail report, where you first list the unit, all its professionals, and its procedures. Then list another drive if you have it in the table.

Any tips on how to do this?

    
asked by anonymous 07.07.2017 / 04:56

1 answer

3

Following this output you requested, you can do this:

<table>
    @foreach (var unidade in Model.GroupBy(i => i.unidade))
    {
        <tr>
            <td>UNIDADE @unidade.Key</td>
        </tr>
        foreach (var profissional in unidade.GroupBy(i => i.profissional))
        {
            <tr>
                <td>PROFISSIONAL @profissional.Key</td>
            </tr>
            foreach (var p in profissional)
            {
                <tr>
                    <td>Procedimento @p.procedimento</td>
                </tr>
            }
        }
    }
</table>
    
07.07.2017 / 06:30