Sum total value of each LINQ category

Asked

Viewed 111 times

2

I have a Shopping List(Shoppingitems) like this:

Descricao: Padaria Campobelo
Valor: -24.00
Data: 06-03-2019
Categoria: Alimentacao

Descricao: Uber tecnologia
Valor: -24.00
Data: 07-03-2019
Categoria: Transporte

Descricao: Uber tecnologia
Valor: -30.00
Data: 08-03-2019
Categoria: Transporte

I would like to know how to get to the category with the highest expense, I tried to do as follows:

var result = (from item in ListShoppingItens
              group item by item.Categoria into cat
              select new ShoppingItens
              {
                  Valor = cat.Sum(x => x.Valor)
             }).ToList();

But it is returning only one category, in the case of travel

My class is like this:

public class ShoppingItens
{
    public enum Category 
    {
        VIAGEM,
        DIVERSAO,
        ALIMENTACAO,
        HOSPEDAGEM,
        VESTUARIO,
        TRANSPORTE,
        HIGIENE,
        SEMCATEGORIA,
    }

    public DateTime Data { get; set; }

    public string Descricao { get; set; }

    public decimal Valor { get; set; }

    public Category Categoria { get; set; }
}
  • because the value is negative? what is the output you want (for example)? because in the list you gave of sample you placed a category that does not exist (supermarket)?

  • The value is negative because it’s the amount he spent on that particular purchase, that is, the money that came out, if there is a positive value it means he received the money.

1 answer

1


I made an example in Console Application to show how to get the grouped value of each category.

var ListShoppingItens = new List<ShoppingItens>
        {
            new ShoppingItens { Data = DateTime.Now, Categoria = ShoppingItens.Category.ALIMENTACAO, Descricao = "TESTE", Valor = 1770 },
            new ShoppingItens { Data = DateTime.Now, Categoria = ShoppingItens.Category.DIVERSAO, Descricao = "TESTE", Valor = 1640 },
            new ShoppingItens { Data = DateTime.Now, Categoria = ShoppingItens.Category.SEMCATEGORIA, Descricao = "TESTE", Valor = 410 },
            new ShoppingItens { Data = DateTime.Now, Categoria = ShoppingItens.Category.ALIMENTACAO, Descricao = "TESTE", Valor = 150 },
            new ShoppingItens { Data = DateTime.Now, Categoria = ShoppingItens.Category.TRANSPORTE, Descricao = "TESTE", Valor = 160 },
            new ShoppingItens { Data = DateTime.Now, Categoria = ShoppingItens.Category.ALIMENTACAO, Descricao = "TESTE", Valor = 106 },
            new ShoppingItens { Data = DateTime.Now, Categoria = ShoppingItens.Category.HIGIENE, Descricao = "TESTE", Valor = 160 },
            new ShoppingItens { Data = DateTime.Now, Categoria = ShoppingItens.Category.HIGIENE, Descricao = "TESTE", Valor = 1044 },
            new ShoppingItens { Data = DateTime.Now, Categoria = ShoppingItens.Category.ALIMENTACAO, Descricao = "TESTE", Valor = 1440 },
            new ShoppingItens { Data = DateTime.Now, Categoria = ShoppingItens.Category.TRANSPORTE, Descricao = "TESTE", Valor = 150 },
            new ShoppingItens { Data = DateTime.Now, Categoria = ShoppingItens.Category.ALIMENTACAO, Descricao = "TESTE", Valor = 1550 },
            new ShoppingItens { Data = DateTime.Now, Categoria = ShoppingItens.Category.DIVERSAO, Descricao = "TESTE", Valor = 10 },
            new ShoppingItens { Data = DateTime.Now, Categoria = ShoppingItens.Category.ALIMENTACAO, Descricao = "TESTE", Valor = 12220 }
        };


        var resultado = from item in ListShoppingItens
                     group item by item.Categoria into cat
                     select new 
                     {
                         Categoria = cat.Key,
                         Total = cat.Sum(x => x.Valor),
                     };


        foreach (var item in resultado)
            Console.WriteLine(item.Categoria + "-" + item.Total);
  • It worked, it was almost there rs. Only one detail in your code, the Shoppingitems class does not have the Total property, so I had to create it, but still thank you.

  • In my code the Total property is anonymous, so it wasn’t necessary to create it. I’m glad it worked. Good codes :)

Browser other questions tagged

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