I have this lambda:
[Route("getliberaitens/{id}")]
public List<LiberacaoItensDTO> GetLibItems(double id)
{
var lista = contexto.Liberacoes
.Join
(
contexto.ItensLibs,
t1 => t1.IdOrcamento,
t2 => t2.IdOrcamento,
(t1, t2) => new { t1, t2 }
)
.Where(a => a.t1.IdOrcamento == a.t2.IdOrcamento && a.t1.IdOrcamento == id)
.GroupBy(gb => new { gb.t1.IdOrcamento })
.Select(item => new LiberacaoItensDTO
{
TotalVenda = item.Sum(a => a.t1.TotalLiquido),
TotalLucro = item.Sum(a => a.t2.Total - (a.t2.Qtde * a.t2.Custo))
}).ToList();
//double totallucro = lista.Sum(t => t.TotalLucro);
lista.ForEach(t => t.TotalLucro = double.Parse(string.Format(new CultureInfo("pt-BR"), "{0:N}", t.TotalLucro)));
return lista;
}
This is the result of Postman
"TotalVenda": 5470,
"TotalLucro": 273.71
It turns out that this result is wrong. For this budget TotalVenda is: 1094 . It arrived at this result, because in Sum, it added by the amount of items, that in this case this sale has 5 items. If I take out the Sum it gives error in t1 and t2. The total profit in this case is 30% of the total of the sale, which does not give the value above. Can someone give me some help on how to solve the SUM question?