Group with Lambda, ASP.NET MVC

Asked

Viewed 110 times

0

I have a question here. I have a action which receives two parameters, two dates. This generates a list, but in this list, the same item is repeating. wanted to be able to make a sum of money. For a better understanding, the system is garage management. So I have Vr(Vehicle Requisition - Class), Mv(Vehicle Movement - Class), and Vehicle - Class.

Class Mv

    public int Id { get; set; }

    public decimal CombustivelAbastecido { get; set; }

The class Vr(Vehicle Requisition) has the vehicle. the report I want to show, receives the two dates, initial, and final, then would show the vehicles requested in that period, a vehicle was requested more than once in that period, and had an Expense "X", so I wanted it to be more or less like this;

inserir a descrição da imagem aqui

I have the Fuel class, which has the value and the description. The Fuel Expense, would be the sum of all Fuel Cost, which has in the class Mv(Vehicle Movement)

The way I’m doing:

public Actionresult Reportariodatadois(Datetime? dataInicio, Datetime? dataFim) {

        ViewBag.dataInicial = dataInicio;
        ViewBag.dataFinal = dataFim;

        if (dataInicio == null && dataFim == null)
        {
            var vrDb = db.VrDb.Where(v => v.DataSolicitacao >= dataInicio && v.DataSolicitacao <= dataFim).OrderBy(v => v.DataSolicitacao).ToList();
            return View(vrDb);
        }

        else
        {
            var vrDb = db.VrDb.Where(v => v.DataSolicitacao >= dataInicio && v.DataSolicitacao <= dataFim && v.Situacao == Situacao.Finalizado).OrderBy(v => v.DataSolicitacao).ToList();


            var data = dataInicio;
            var dataF = dataFim;

            var tot = db.VrDb.Sum(v => v.Mv.Consumo); //Mostra o Total gasto

            if (tot == 0)
            {
                ViewBag.Total = "0";
            }
            else
            {
                ViewBag.Total = tot;
            }

            var abastecido = db.VrDb.Sum(v => v.Mv.CombustivelAbastecido);
            if (abastecido == 0)
            {
                ViewBag.Abastecido = "0";
            }
            else
            {
                ViewBag.Abastecido = abastecido;
            }



            var TotalValorEmDinheiro = db.VrDb.Sum(v => v.Veiculo.Combustivel.Preco * v.Mv.CombustivelAbastecido);

            if (TotalValorEmDinheiro == 0)
            {
                ViewBag.TotalValorEmDinheiro = "0";
            }
            else
            {
                ViewBag.TotalValorEmDinheiro = TotalValorEmDinheiro;
            }


            return View(vrDb);
            //return RedirectToAction("RelatorioTotalPorData", vrDb);

        }

    }

This way you bring me this way inserir a descrição da imagem aqui

That is, you’re repeating the same vehicle. Not to mention that the expense, you’re bringing me the same thing.

  • 1

    You can post your class structures to analyze?

  • 1

    If you want it to be grouped by vehicle do : db.VrDb.Where(v => v.DataSolicitacao >= dataInicio && v.DataSolicitacao <= dataFim && v.Situacao == Situacao.Finalizado).OrderBy(v => v.DataSolicitacao).GroupBy(v => v.Descricao).ToList(); Or instead of the description the vehicle identifier.

  • This way always this error: The model item inserted in the dictionary is of type'System.Collections.Generic.List1[System.Linq.IGrouping2[System.String,Garagemmodel.Vr]]', but this dictionary requires a 'System.Collections.Generic.Ienumerable`1[Garagemmodel.Vr]' item. var vrDb = db.VrDb.Where(v => v.Requesteddate >= dateApplication && v.Requested dateApplication <= dateFim && v.Status == Status.Completed). Orderby(v => v.Date request). Groupby(v => v.Veiculo.Description). Tolist(); and where the groupBy tag is, it’s not just v.Description, V is Vr, so it’s v.Veiculo.Description

No answers

Browser other questions tagged

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