我需要将此语句SQL转换为LINQ:
SELECT SUM(L.total_linea) AS Total, I.porcentaje
FROM linea_pedidos L
INNER JOIN iva I ON L.id_iva = I.id_iva
GROUP BY L.id_iva
ORDER BY I.porcentaje DESC
目前我已经这样做了,但我不能继续前进。
from l in linea_pedidos
join i in iva on l.id_iva equals i.id_iva
select new
{
l.id_iva,
i.porcentaje,
total = l.total_linea * i.porcentaje / 100
} into s
group s by s.id_iva into g
select g.Sum(t => t.total)
答案 0 :(得分:2)
您需要在分组中加入porcentaje。
如果您考虑一下,您的SQL查询与
相同SELECT SUM(L.total_linea * I.porcentaje / 100) AS Total, I.porcentaje
FROM linea_pedidos L
INNER JOIN iva I ON L.id_iva = I.id_iva
GROUP BY L.id_iva , I.porcentaje
ORDER BY I.porcentaje DESC
你可以翻译成
from l in linea_pedidos
join i in iva on l.id_iva equals i.id_iva
select new
{
l.id_iva,
i.porcentaje,
total = l.total_linea * i.porcentaje / 100
} into s
group s by new { s.id_iva , s.porcentaje } into g
orderby g.Key.porcentaje descending
select new { Total = g.Sum(t => t.total) , g.Key.porcentaje }