LINQ with JOIN,GROUP BY,ORDER BY,SUM,乘以两个List的值

时间:2018-06-10 23:59:45

标签: c# .net linq linq-to-sql

我需要将此语句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)

1 个答案:

答案 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  }