我需要帮助将语句从LINQ转换为SQL
SQL
SELECT MONTH(ind_receita.lad_ins_date) as 'Month', SUM(ind_receita.valor) as Monthly_Value
FROM ind_receita
WHERE YEAR(ind_receita.lad_ins_date) = 2014
GROUP BY MONTH(ind_receita.lad_ins_date)
WITH ROLLUP
我知道如何在LINQ中执行所有语句,但我对WITH ROLLUP
LINQ
var query = (from p in _repositorio.GetReceitas()
where p.DataHoraCriacaoRegistro.Year == 2014
group p by new { p.DataHoraCriacaoRegistro.Month } into grp
select new ReceitaPorGrupoProduto
{
// Column with the alias 'Monthly_Value'
ValorReceita = grp.Sum(p => p.Valor)
// Column with the alias 'Month'
DataHora = grp.Key.Month
}).ToList();
但是如何将WITH ROLLUP
放入我的LINQ
?
Rollup会给我一个累加器,所以,如果我们可以在不为LINQ制作ROLLUP语句的情况下完成它,那就没关系。
答案 0 :(得分:2)
这里提供了非常有趣的解决方案
https://blogs.msdn.microsoft.com/mitsu/2007/12/21/playing-with-linq-grouping-groupbymany/
它描述了如何通过几个属性执行groupbby。即:
var result = customers.GroupByMany(c => c.Country, c => c.City);
结果,您将获得可以简单地转换为平面列表的分层结构。我希望你能为你的情况采用这个。