You would need to group by date, or direct month and then select on top of the generated list by multiplying the value by the amount of liters.
I made a console application to illustrate your case, I hope it helps or give you a light, follows below the DTO used for simulation of your bank:
public class Valores
{
public double ValorPago { get; set; }
public DateTime DataAbastecimento { get; set; }
public double QtdLitros { get; set; }
}
and the program class with the code.
class Program
{
static void Main(string[] args)
{
//carrega valores aleatórios
List<Valores> valores = new List<Valores>() {
new Valores { DataAbastecimento = new DateTime(2020,1,20), ValorPago = 3.00, QtdLitros = 20 },
new Valores { DataAbastecimento = new DateTime(2020,1,20), ValorPago = 3.20, QtdLitros = 21 },
new Valores { DataAbastecimento = new DateTime(2020,1,20), ValorPago = 3.40, QtdLitros = 23 },
new Valores { DataAbastecimento = new DateTime(2020,2,20), ValorPago = 3.50, QtdLitros = 15 },
new Valores { DataAbastecimento = new DateTime(2020,2,20), ValorPago = 3.00, QtdLitros = 30 },
new Valores { DataAbastecimento = new DateTime(2020,3,20), ValorPago = 3.80, QtdLitros = 22 },
new Valores { DataAbastecimento = new DateTime(2020,3,20), ValorPago = 3.45, QtdLitros = 27 },
new Valores { DataAbastecimento = new DateTime(2020,5,20), ValorPago = 3.60, QtdLitros = 29 },
new Valores { DataAbastecimento = new DateTime(2020,4,20), ValorPago = 3.70, QtdLitros = 18 },
new Valores { DataAbastecimento = new DateTime(2020,6,20), ValorPago = 3.50, QtdLitros = 5 },
new Valores { DataAbastecimento = new DateTime(2020,8,20), ValorPago = 3.80, QtdLitros = 45 },
new Valores { DataAbastecimento = new DateTime(2020,9,20), ValorPago = 3.90, QtdLitros = 20 },
new Valores { DataAbastecimento = new DateTime(2020,10,20), ValorPago = 3.50, QtdLitros = 30 },
new Valores { DataAbastecimento = new DateTime(2020,11,20), ValorPago = 3.70, QtdLitros = 12 },
new Valores { DataAbastecimento = new DateTime(2020,12,20), ValorPago = 3.80, QtdLitros = 18 },
new Valores { DataAbastecimento = new DateTime(2020,3,20), ValorPago = 3.80, QtdLitros = 12 },
new Valores { DataAbastecimento = new DateTime(2020,12,20), ValorPago = 3.85, QtdLitros = 45 },
new Valores { DataAbastecimento = new DateTime(2020,11,20), ValorPago = 3.50, QtdLitros = 50 },
new Valores { DataAbastecimento = new DateTime(2020,5,20), ValorPago = 3.60, QtdLitros = 40 },
new Valores { DataAbastecimento = new DateTime(2020,7,20), ValorPago = 3.45, QtdLitros = 30 },
new Valores { DataAbastecimento = new DateTime(2020,8,20), ValorPago = 3.10, QtdLitros = 12 },
new Valores { DataAbastecimento = new DateTime(2020,6,20), ValorPago = 3.15, QtdLitros = 18 },
new Valores { DataAbastecimento = new DateTime(2020,9,20), ValorPago = 3.30, QtdLitros = 16 },
new Valores { DataAbastecimento = new DateTime(2020,10,20), ValorPago = 3.45, QtdLitros = 19 },
new Valores { DataAbastecimento = new DateTime(2020,12,20), ValorPago = 3.78, QtdLitros = 12 },
new Valores { DataAbastecimento = new DateTime(2020,11,20), ValorPago = 3.85, QtdLitros = 21 },
new Valores { DataAbastecimento = new DateTime(2020,4,20), ValorPago = 3.98, QtdLitros = 24 },
new Valores { DataAbastecimento = new DateTime(2020,11,20), ValorPago = 3.45, QtdLitros = 26 },
};
var valoresAgrupados = valores.Where(w => w.DataAbastecimento.Year == DateTime.Now.Date.Year)
.GroupBy(g => g.DataAbastecimento.Month).Select(s => new { Month = s.Key, Valor = s.Sum(soma => soma.QtdLitros * soma.ValorPago)});
foreach (var item in valoresAgrupados)
{
Console.WriteLine($"Mês: {item.Month} - valor: ${item.Valor}");
}
}
}
Expected result:
Mês: 1 - valor: $205,4
Mês: 2 - valor: $142,5
Mês: 3 - valor: $222,35
Mês: 5 - valor: $248,4
Mês: 4 - valor: $162,12
Mês: 6 - valor: $74,19999999999999
Mês: 8 - valor: $208,2
Mês: 9 - valor: $130,8
Mês: 10 - valor: $170,55
Mês: 11 - valor: $389,95
Mês: 12 - valor: $287,01
Mês: 7 - valor: $103,5
I’m sorry, your question has nothing to do with
JSON
,Visual Studio
orwebapi
... is a matter of Linus and lambda queries.– Leandro Angelo
@Leandroangelo, how to proceed since then, you could assist me in the matter of Lillian and lambda queries ?
– Cyberlacs
And yet in its proposal, the Json does not make sense if the months are not arranged in another object, which would be the year... Unless you want to add up all the "Windows" of all the years...
– Leandro Angelo