Show Groupby List using Lambda in view. ASP.NET MVC

Asked

Viewed 137 times

1

Well, I’m having a little bit of a problem here. I consult at the bank using Lambda, and use the tag GroupBy,query is bringing objects correctly, the problem, is that I can’t show in view. gives an error when I use IEnumerable<> and when I try to use List bug tbm. however, if I don’t use the GroupBy, shows on screen, using the IEnumerable<>, 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 inserted in the dictionary is from type'System.Collections.Generic.List1[System.Linq.IGrouping2[System.Int32, Garagemmodel. Vr]', but this dictionary requires an item like 'System.Collections.Generic.Ienumerable`1[Garagemmodel.Vr]'.

2 answers

1


When using the GroupBy in your consultation is modifying the data type of the result, so it is defined that the information should be in the format IEnumerable<GaragemModel.Vr> you can’t bring it together with another guy.


Try doing it this way:

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>

Note that the code has not been tested, it may need some adjustments.

  • I tried this way, however, the part where it is: g.Placa. g.Descricao... etc, from the error, because it does not have this property for the "g". appears only properties like g.Groupbu(), g.Tolist().. etc

1

Change the model type to IGrouping<int, GaragemModel.Vr> that there will be no change in behavior and will fail to make a mistake.

  • I changed it to Igrouping<string, Garagemmodel.Vr>, but it still has the error

  • Fix the code, correct is IGrouping<int, GaragemModel.Vr>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.