I made a sum with lambda and is not working as expected

Asked

Viewed 112 times

2

I need to make a general sum for total and profit. If I made a sale with 4 items, I’m seeing in my grid four sum and that’s not it, even if it repeated 4 times, but it should be just a sum. I need this to generate a graph where someone will see the total of seeing and profit(slice). As is my business. A service to be consumed with the information of the items of sale and an App (Xamarin.Forms) that will consume this service. I have my Model:

[Table("ITENSLIB")]
    public class ItensLib
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        [Column("ID_ITENSLIB")]
        public int IdItensLib { get; set; }
        [Column("ID_ORCAMENTO")]
        public double IdOrcamento { get; set; }
        [Column("ID_PRODUTO")]
        public int IdProduto { get; set; }
        [Column("PRODUTO")]
        public string Produto { get; set; }
        [Column("QTDE")]
        public double Qtde { get; set; }
        [Column("UNITARIO")]
        public double Unitario { get; set; }
        [Column("CUSTO")]
        public double Custo { get; set; }
        [Column("MINIMO")]
        public double Minimo { get; set; }
        [Column("TOTAL")]
        public double Total { get; set; }
        [Column("CUSTODIARIO")]
        public double CustoDiario { get; set; }
        [Column("FABRICANTE")]
        public string Fabricante { get; set; }
        [Column("ULT_CONDICAO")]
        public float UltCondicao { get; set; }
        [Column("PROGRAMA")]
        public string Programa { get; set; }
        [Column("NOME_PC")]
        public string NomePc { get; set; }
        [Column("NOME_PROCEDURE")]
        public string NomeProcedure { get; set; }
        [Column("Flag_Vencido")]
        public byte FlagVencido { get; set; }
        [Column("TipoVenda")]
        public string TipoVenda { get; set; }
        [Column("VENDA_VISTA")]
        public double VendaVista { get; set; }
        [Column("MARGEM_AVISTA")]
        public double MargemAvista { get; set; }
        [Column("QTDE_NEG_AVISTA")]
        public double QtdNegAvista { get; set; }
        [Column("VENDA_PRAZO")]
        public double VendaPrazo { get; set; }
        [Column("PM_PRAZO")]
        public double PmPrazo { get; set; }
        [Column("MARGEM_PRAZO")]
        public double MargemPrazo { get; set; }
        [Column("JUROS_PRAZO")]
        public double JurosPrazo { get; set; }
        [Column("QTDE_PRAZO")]
        public double QtdePrazo { get; set; }
        [Column("VENDA_VISTA_ANT")]
        public double VendaVistaAnt { get; set; }
        [Column("MARGEM_AVISTA_ANT")]
        public double MargemAvistaAnt { get; set; }
        [Column("QTDE_NEG_AVISTA_ANT")]
        public double QtdeNegAvistaAnt { get; set; }
        [Column("VENDA_PRAZO_ANT")]
        public double VendaPrazoAnt { get; set; }
        [Column("PM_PRAZO_ANT")]
        public double PmPrazoAnt { get; set; }
        [Column("MARGEM_PRAZO_ANT")]
        public double MargemPrazoAnt { get; set; }
        [Column("JUROS_PRAZO_ANT")]
        public double JurosPrazoAnt { get; set; }
        [Column("QTDE_PRAZO_ANT")]
        public float QtdePrazoAnt { get; set; }
        [Column("VENDA_VISTA_ANT1")]
        public double VendaVistaAnt1 { get; set; }
        [Column("MARGEM_AVISTA_ANT1")]
        public double MargemAvistaAnt1 { get; set; }
        [Column("QTDE_NEG_AVISTA_ANT1")]
        public double QtdeNegAvistaAnt1 { get; set; }
        [Column("VENDA_PRAZO_ANT1")]
        public double VendaPrazoAnt1 { get; set; }
        [Column("PM_PRAZO_ANT1")]
        public double PmPrazoAnt1 { get; set; }
        [Column("MARGEM_PRAZO_ANT1")]
        public double MargemPrazoAnt1 { get; set; }
        [Column("JUROS_PRAZO_ANT1")]
        public double JurosPrazoAnt1 { get; set; }
        [Column("QTDE_PRAZO_ANT1")]
        public double QtdePrazoAnt1 { get; set; }

    }

Well, for this model I generated a DTO and in this DTO I created two fields, Profit and Summation. These fields(both double) should bring the overall sum. Here’s how I did to try to generate a sum and it’s not working:

[Route("itens/{id}")]
        public List<ItensLibDTO> GetItensLib(int id)
        {
            var lista = contexto.ItensLibs
                .Where(itens => itens.IdOrcamento == id)
                .Select(item => new ItensLibDTO
                {
                    Produto = item.Produto,
                    Qtde = item.Qtde.ToString(),
                    Unitario = item.Unitario.ToString(),
                    MargemAvista = item.MargemAvista.ToString(),
                    Custo = item.Custo.ToString(),
                    CustoDiario = item.CustoDiario.ToString(),
                    UltCondicao = item.UltCondicao.ToString(),
                    Total = item.Total.ToString(),
                    Lucro = item.Total - (item.Qtde * item.Custo),
                    SomaTotal = item.Total,
                    DadosItens = ""
                }).ToList();

            lista.ForEach(e => e.UltCondicao = new DateTime(1800, 12, 28).AddDays(float.Parse(e.UltCondicao)).ToString("dd/MM/yy"));

            lista.ForEach(e => e.Unitario = string.Format(new CultureInfo("pt-BR"), "{0:N}", double.Parse(e.Unitario, new CultureInfo("en").NumberFormat)));
            lista.ForEach(e => e.Custo = string.Format(new CultureInfo("pt-BR"), "{0:N}", double.Parse(e.Custo, new CultureInfo("en").NumberFormat)));
            lista.ForEach(e => e.CustoDiario = string.Format(new CultureInfo("pt-BR"), "{0:N}", double.Parse(e.CustoDiario, new CultureInfo("en").NumberFormat)));

            lista.ForEach(e => e.Total = string.Format(new CultureInfo("pt-BR"), "{0:N}", double.Parse(e.Total, new CultureInfo("en").NumberFormat)));

            lista.ForEach(e => e.DadosItens = "Qtde " +  e.Qtde.ToString() + " " + "Uni. " + e.Unitario.ToString() + " " + "Marg. " + e.MargemAvista.ToString() + 
                          " " + "Custo " + e.Custo.ToString() + " " + "C.Dia " + e.CustoDiario.ToString() + " " + "U.Cond. " + e.UltCondicao.ToString() +
                          " " + "Total " + e.Total.ToString());
            lista.ForEach(e => e.Lucro = double.Parse(string.Format(new CultureInfo("pt-BR"), "{0:N}", e.Lucro)));

            lista.Sum(e => e.Lucro);//ISSO AQUI NÃO ESTÁ FUNCIONANDO
            return lista;

I didn’t put the code DTO, because it would be redundant, since it resembles the Model, with fewer fields and two fields double servants.

EDIT'

{
        "Produto": "FARELO DE SOJA 50KG ",
        "Qtde": "7",
        "Unitario": "69,57",
        "MargemAvista": "0",
        "Custo": "52,02",
        "CustoDiario": "52,02",
        "UltCondicao": "03/04/17",
        "Total": "486,97",
        "DadosItens": "Qtde 7 Uni. 69,57 Marg. 0 Custo 52,02 C.Dia 52,02 U.Cond. 03/04/17 Total 486,97",
        "AutorizouReceberAtrazado": null,
        "FlagLiberacao": 0,
        "Lucro": 122.83,
        "SomaTotal": 486.9667108089
    },
    {
        "Produto": "FARELO DE ALGODAO 28 % PROT. 50KG ",
        "Qtde": "3",
        "Unitario": "48,01",
        "MargemAvista": "0",
        "Custo": "37,75",
        "CustoDiario": "37,61",
        "UltCondicao": "13/05/17",
        "Total": "144,02",
        "DadosItens": "Qtde 3 Uni. 48,01 Marg. 0 Custo 37,75 C.Dia 37,61 U.Cond. 13/05/17 Total 144,02",
        "AutorizouReceberAtrazado": null,
        "FlagLiberacao": 0,
        "Lucro": 30.77,
        "SomaTotal": 144.0243461394
    },
    {
        "Produto": "SAL MOIDO 25KG POTIGUAR ",
        "Qtde": "3",
        "Unitario": "11,56",
        "MargemAvista": "0",
        "Custo": "9,73",
        "CustoDiario": "9,73",
        "UltCondicao": "30/01/17",
        "Total": "34,68",
        "DadosItens": "Qtde 3 Uni. 11,56 Marg. 0 Custo 9,73 C.Dia 9,73 U.Cond. 30/01/17 Total 34,68",
        "AutorizouReceberAtrazado": null,
        "FlagLiberacao": 0,
        "Lucro": 5.49,
        "SomaTotal": 34.6792726521
    },
    {
        "Produto": "MATSUDA TOP MILK VITAMINADO 30KG LOTE (SL)",
        "Qtde": "1",
        "Unitario": "82,31",
        "MargemAvista": "0",
        "Custo": "57,46",
        "CustoDiario": "57,35",
        "UltCondicao": "08/05/17",
        "Total": "82,31",
        "DadosItens": "Qtde 1 Uni. 82,31 Marg. 0 Custo 57,46 C.Dia 57,35 U.Cond. 08/05/17 Total 82,31",
        "AutorizouReceberAtrazado": null,
        "FlagLiberacao": 0,
        "Lucro": 24.85,
        "SomaTotal": 82.314492627400014
    },
    {
        "Produto": "ADUBO UREIA 45-00-00 50KG MOSAIC LOTE (SL)",
        "Qtde": "5",
        "Unitario": "69,20",
        "MargemAvista": "0",
        "Custo": "51,25",
        "CustoDiario": "50,77",
        "UltCondicao": "30/05/17",
        "Total": "346,01",
        "DadosItens": "Qtde 5 Uni. 69,20 Marg. 0 Custo 51,25 C.Dia 50,77 U.Cond. 30/05/17 Total 346,01",
        "AutorizouReceberAtrazado": null,
        "FlagLiberacao": 0,
        "Lucro": 89.76,
        "SomaTotal": 346.01224777000004
    }
  • you do not have to put the function return on some variable ?

  • x = lista.Sum(e => e.Lucro);

  • it returns in the var list. And list is what goes in the service when prompted

  • I did a test here and the sum() function will return a value... You are not putting this value anywhere

  • @Rovannlinhalis, the list variable already loads this value. Sum is not totaling. I’ll show Postman and see how it is. I’ll edit and post the Postman result. Note that I have different Profit for each row and it should be a single value, the total of all profits, but I will create another Property and put the sum of profit into it. You may be right.

1 answer

3


If I understood correctly, you want that in each Itenslibdto has the sum value of all items, even if it is repeated, soon would so:

double lucroTotal = lista.Sum(e => e.Lucro);

lista.ForEach(e => e.LucroTotal = lucroTotal); 

of course you must have the Lucrototal field inside the DTO

Browser other questions tagged

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