Show GroupBy List using Lambda, in the view. ASP.NET MVC

1

Well, I have a little big problem here. I make the query in the database using Lambda , and I use the GroupBy tag, the query is bringing objects correctly, the problem is that I can not show in the view. gives an error when I use IEnumerable<> and when I try to use List of error tbm. but if I do not use GroupBy , it shows on screen, using IEnumerable<> , it follows code:

Controller :

public ActionResult RelatorioDatadois(DateTime? dataInicio, DateTime? dataFim)
{
    ViewBag.dataInicial = dataInicio;
    ViewBag.dataFinal = dataFim;

    if (dataInicio == null && dataFim == null)
    {
        return View();
    }
    else
    {
        var vrDb = db.VrDb.Where(v => v.DataSolicitacao >= dataInicio && v.DataSolicitacao <= dataFim && v.Situacao == Situacao.Finalizado).GroupBy(v => v.Veiculo.Id).ToList();
        var data = dataInicio;
        var dataF = dataFim;
        return View(vrDb);
    }
}

View :

@model IEnumerable<GaragemModel.Vr>

<div class="panel-heading">

    <ul class="list-inline">
        <li> <h4>Período:</h4></li>
        <li>@ViewBag.dataInicial Até</li>
        <li> @ViewBag.dataFinal</li>
    </ul>
</div>    

<table class="table table-striped table-bordered table-hover" id="dataTables-example">
    <thead>
        <tr>
            <th>
                Placa
            </th>

            <th>
                Descrição
            </th>
            <th>
                Combustivel Gasto
            </th>

            <th>
                Km Percorrido
            </th>
            <th>
                Gasto
            </th>    
        </tr>
    </thead>
    <tbody>    
        @foreach (var item in Model)
        {
            <tr class="odd gradeX">
                <td>
                    @Html.DisplayFor(modelItem => item.Veiculo.Placa)
                </td>

                <td>
                    @Html.DisplayFor(modelItem => item.Veiculo.Descricao)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Mv.Consumo) Litros
                </td>

                <td>
                    @Html.DisplayFor(modelItem => item.Mv.QuilometrosPercorridos) Km
                </td>
                <td>
                    R$ @ViewBag.TotalValorEmDinheiro
                </td>
            </tr>
        }    
    </tbody>
</table>

The error When I use IEnumerable<> :

  

The template item entered in the dictionary is from   type 'System.Collections.Generic.List 1[System.Linq.IGrouping 2 [System.Int32, GaragemModel.Vr]]',   but this dictionary requires an item of type   'System.Collections.Generic.IEnumerable'1 [GaragemModel.Vr]'.

    
asked by anonymous 14.08.2018 / 16:58

2 answers

1

When you use GroupBy in your query is to modify the data type of the result, so if you are defining that the information should be in the format IEnumerable<GaragemModel.Vr> you can not bring it grouped with another type.

Try doing the following:

Controller

var vrDb = (from x in db.VrDb
            where x.DataSolicitacao >= dataInicio
            && v.DataSolicitacao <= dataFim
            && v.Situacao == Situacao.Finalizado
            group x by x.Veiculo.Id into g
            select new
            {
                g.Placa,
                g.Descricao,
                g.Consumo,
                g.QuilometrosPercorridos
            });

View

@model IEnumerable<dynamic>

<div class="panel-heading">
    <ul class="list-inline">
        <li> <h4>Período:</h4></li>
        <li>@ViewBag.dataInicial Até</li>
        <li> @ViewBag.dataFinal</li>
    </ul>
</div>    

<table class="table table-striped table-bordered table-hover" id="dataTables-example">
    <thead>
        <tr>
            <th>
                Placa
            </th>

            <th>
                Descrição
            </th>
            <th>
                Combustivel Gasto
            </th>

            <th>
                Km Percorrido
            </th>
            <th>
                Gasto
            </th>    
        </tr>
    </thead>
    <tbody>    
        @foreach (var item in Model)
        {
            <tr class="odd gradeX">
                <td>
                    @Html.DisplayFor(modelItem => item.Placa)
                </td>

                <td>
                    @Html.DisplayFor(modelItem => item.Descricao)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Consumo) Litros
                </td>

                <td>
                    @Html.DisplayFor(modelItem => item.QuilometrosPercorridos) Km
                </td>
                <td>
                    R$ @ViewBag.TotalValorEmDinheiro
                </td>
            </tr>
        }    
    </tbody>
</table>

Please note that the code has not been tested, you may need some adjustments.

    
14.08.2018 / 17:32
0

Change the template type to IGrouping<int, GaragemModel.Vr> that there will be no behavior change and will no longer give error.

    
14.08.2018 / 18:13