3
I got 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;
}
That’s the result caught in the Postman
"TotalVenda": 5470,
"TotalLucro": 273.71
It turns out that this result is wrong. For this budget the Totalvenda is : 1094. It came to this result, because in Sum, he added by the amount of items, in this case this sale has 5 items. If I withdraw the Sum gives error in t1 and t2. The total profit in this case is 30% of the total sale, which does not give the above value. Can someone give me a hand on how to resolve the issue of SUM?
for you remove the sum would have to include in the group by , if Totalliquido is a list with the same velars you could also use the first.
– Marco Souza
@Marconciliosouza, and how would I do that? See that Totalvenda and Totallucro are part of Liberacaoitensdto and the other fields come from Models Release and Itenslib. And how would I do that? How do you include it in Groupby? I have grouped, for when I present Chart, the share of profit did not repeat itself according to the amount of items, as was happening.
– pnet
to put in group by simply include in new { Gb.t1.Marking , Gb. " _ " . Totalliquid } .. and select you access with item.key.Totalliquid
– Marco Souza
is a little confused what you want to do, would mount the query in sql and put in question how it would be ?
– Marco Souza
The comment went wrong. The last way is working. If you want to answer, I mark the answer.
– pnet
post your solution... not give to understand your structure.
– Marco Souza
@Marconciliosouza, as I said it’s okay.
– pnet
I believe it was the way I replied, right ?
– Marco Souza
See if the answer is according to what you had commented.
– Marco Souza