【发布时间】:2010-12-13 20:13:34
【问题描述】:
我正在尝试将下面的 SQL 查询转换为 Linq to SQL
select Categorias.IdCategoria, Categorias.Nome, SUM(lancamentos.valor)
from lancamentos
left outer join Categorias on Lancamentos.IdCategoria = Categorias.IdCategoria
where Month(DataLancamento) = 11
and Credito = 1
and Lancamentos.Ocultar = 0
group by Categorias.IdCategoria, Categorias.Nome
这就是我所做的
from lancamento in Lancamentos
where lancamento.Credito == true
&& lancamento.Ocultar == false
&& lancamento.DataLancamento.Month == 10
join categoria in Categorias on lancamento.IdCategoria equals categoria.IdCategoria into temp
from lancamentoJoinCategoria in temp.DefaultIfEmpty()
group lancamentoJoinCategoria by new { lancamentoJoinCategoria.IdCategoria, lancamentoJoinCategoria.Nome } into x
select new {
IdCategoria = (int?)x.Key.IdCategoria
, Nome = x.Key.Nome
}
如何将 SUM(lancamentos.valor) 添加到上面的 linq to sql 中?
【问题讨论】:
-
我不明白你想要做的加入。你制定它的方式你想要得到 lancamentos 的总和,即使这些 lancamentos 不存在类别。因此,对于所有未分类的 lancamentos,您会得到一个额外的结果行,例如“NULL,NULL,42378”,对吗?这真的是你的意图还是你想要所有空类别的行像“24,SomeName,NULL”?
标签: c# linq linq-to-sql tsql