1

我需要进行两个左连接并从这两个左连接中求和/计数数据。

首先我尝试了一个左连接(ValorEfetuado)。该表可能有数据,如果有数据,我需要对其求和。

这是带有一个左连接和它生成的选择的原始查询(我认为非常简单)

var query = from l in db.Lancamentos
                        join tl in db.TiposLancamentos on l.IDTipoLancamento equals tl.IDTipoLancamento
                        join ve in db.ValoresEfetuados on l.IDLancamento equals ve.IDLancamento into ve_leftjoin
                        from _ve in ve_leftjoin.DefaultIfEmpty()

                    where (l.Excluido == false && l.IDConta == IDConta)

                    group _ve by new
                    {
                        TipoLancamento = tl.Descricao,
                        DataVencimento = l.DataVencimento,
                        IDLancamento = l.IDLancamento,
                        IDConta = l.IDConta,
                        Valor = l.Valor
                    } into grupo
                    select new _LancamentoGrid
                    {
                        IDConta = grupo.Key.IDConta,
                        IDLancamento = grupo.Key.IDLancamento,
                        Descricao = grupo.Key.TipoLancamento,
                        Valor = grupo.Key.Valor,
                        DataVencimento = grupo.Key.DataVencimento,
                        ValorEfetuado = grupo.Sum(_vesum => _vesum.Valor == null ? 0 : _vesum.Valor),
                    };

那就是选择

SELECT 
[GroupBy1].[K1] AS [IDLancamento], 
[GroupBy1].[K2] AS [IDConta], 
[GroupBy1].[K5] AS [Descricao], 
[GroupBy1].[K3] AS [Valor], 
[GroupBy1].[K4] AS [DataVencimento], 
[GroupBy1].[A1] AS [C1]
FROM ( SELECT 
    [Filter2].[K1] AS [K1], 
    [Filter2].[K2] AS [K2], 
    [Filter2].[K3] AS [K3], 
    [Filter2].[K4] AS [K4], 
    [Filter2].[K5] AS [K5], 
    SUM([Filter2].[A1]) AS [A1]
    FROM ( SELECT 
        [Filter1].[IDLancamento] AS [K1], 
        [Filter1].[IDConta] AS [K2], 
        [Filter1].[Valor] AS [K3], 
        [Filter1].[DataVencimento] AS [K4], 
        [Filter1].[Descricao] AS [K5], 
        CASE WHEN ([Extent3].[Valor] IS NULL) THEN cast(0 as decimal(18)) ELSE [Extent3].[Valor] END AS [A1]
        FROM   (SELECT [Extent1].[IDLancamento] AS [IDLancamento], [Extent1].[IDConta] AS [IDConta], [Extent1].[Valor] AS [Valor], [Extent1].[DataVencimento] AS [DataVencimento], [Extent2].[Descricao] AS [Descricao]
            FROM  [fin].[Lancamento] AS [Extent1]
            INNER JOIN [fin].[TipoLancamento] AS [Extent2] ON [Extent1].[IDTipoLancamento] = [Extent2].[IDTipoLancamento]
            WHERE 0 = [Extent1].[Excluido] ) AS [Filter1]
        LEFT OUTER JOIN [fin].[ValorEfetuado] AS [Extent3] ON [Filter1].[IDLancamento] = [Extent3].[IDLancamento]
        WHERE [Filter1].[IDConta] = @p__linq__0
    )  AS [Filter2]
    GROUP BY [K1], [K2], [K3], [K4], [K5]
)  AS [GroupBy1]

就是这样,分组和总和。现在问题来了。当我在查询中加入新的左连接并按它分组时,我得到了一个有效的恶魔查询。很大。并且是低效的。就像在这个例子中

var query = from l in db.Lancamentos
                        join tl in db.TiposLancamentos on l.IDTipoLancamento equals tl.IDTipoLancamento
                        join ve in db.ValoresEfetuados on l.IDLancamento equals ve.IDLancamento into ve_leftjoin
                        from _ve in ve_leftjoin.DefaultIfEmpty()
                        join ld in db.LancamentosDocumentos on l.IDLancamento equals ld.IDLancamento into ld_leftjoin
                        from _ld in ld_leftjoin.DefaultIfEmpty()
                        where (l.Excluido == false && l.IDConta == IDConta)

                    group new { _ve, _ld } by new
                    {
                        TipoLancamento = tl.Descricao,
                        DataVencimento = l.DataVencimento,
                        IDLancamento = l.IDLancamento,
                        IDConta = l.IDConta,
                        Valor = l.Valor
                    } into grupo
                    select new _LancamentoGrid
                    {
                        IDConta = grupo.Key.IDConta,
                        IDLancamento = grupo.Key.IDLancamento,
                        Descricao = grupo.Key.TipoLancamento,
                        Valor = grupo.Key.Valor,
                        DataVencimento = grupo.Key.DataVencimento,
                        ValorEfetuado = grupo.Sum(_vesum => _vesum._ve.Valor == null ? 0 : _vesum._ve.Valor),
                        QtdContaDocumento = grupo.Count(_ldcount => _ldcount._ld.IDLancamento != null)
                    };

这导致一个丑陋的巨大查询,简单的剂量返回我要求的

SELECT 
[Project4].[IDLancamento] AS [IDLancamento], 
[Project4].[IDConta] AS [IDConta], 
[Project4].[Descricao] AS [Descricao], 
[Project4].[Valor] AS [Valor], 
[Project4].[DataVencimento] AS [DataVencimento], 
[Project4].[C1] AS [C1], 
[Project4].[C2] AS [C2]
FROM ( SELECT 
    [Project3].[IDLancamento] AS [IDLancamento], 
    [Project3].[IDConta] AS [IDConta], 
    [Project3].[Valor] AS [Valor], 
    [Project3].[DataVencimento] AS [DataVencimento], 
    [Project3].[Descricao] AS [Descricao], 
    [Project3].[C1] AS [C1], 
    (SELECT 
        COUNT(1) AS [A1]
        FROM   (SELECT [Extent9].[IDLancamento] AS [IDLancamento1], [Extent9].[IDConta] AS [IDConta], [Extent9].[Valor] AS [Valor1], [Extent9].[DataVencimento] AS [DataVencimento], [Extent10].[Descricao] AS [Descricao]
            FROM   [fin].[Lancamento] AS [Extent9]
            INNER JOIN [fin].[TipoLancamento] AS [Extent10] ON [Extent9].[IDTipoLancamento] = [Extent10].[IDTipoLancamento]
            LEFT OUTER JOIN [fin].[ValorEfetuado] AS [Extent11] ON [Extent9].[IDLancamento] = [Extent11].[IDLancamento]
            WHERE 0 = [Extent9].[Excluido] ) AS [Filter4]
        INNER JOIN [fin].[LancamentoDocumento] AS [Extent12] ON [Filter4].[IDLancamento1] = [Extent12].[IDLancamento]
        WHERE ([Filter4].[IDConta] = @p__linq__0) AND (([Project3].[Descricao] = [Filter4].[Descricao]) OR (([Project3].[Descricao] IS NULL) AND ([Filter4].[Descricao] IS NULL))) AND ([Project3].[DataVencimento] = [Filter4].[DataVencimento]) AND ([Project3].[IDLancamento] = [Filter4].[IDLancamento1]) AND ([Project3].[IDConta] = [Filter4].[IDConta]) AND ([Project3].[Valor] = [Filter4].[Valor1]) AND ([Extent12].[IDLancamento] IS NOT NULL)) AS [C2]
    FROM ( SELECT 
        [Project2].[IDLancamento] AS [IDLancamento], 
        [Project2].[IDConta] AS [IDConta], 
        [Project2].[Valor] AS [Valor], 
        [Project2].[DataVencimento] AS [DataVencimento], 
        [Project2].[Descricao] AS [Descricao], 
        (SELECT 
            SUM([Filter3].[A1]) AS [A1]
            FROM ( SELECT 
                CASE WHEN ([Extent7].[Valor] IS NULL) THEN cast(0 as decimal(18)) ELSE [Extent7].[Valor] END AS [A1]
                FROM    [fin].[Lancamento] AS [Extent5]
                INNER JOIN [fin].[TipoLancamento] AS [Extent6] ON [Extent5].[IDTipoLancamento] = [Extent6].[IDTipoLancamento]
                LEFT OUTER JOIN [fin].[ValorEfetuado] AS [Extent7] ON [Extent5].[IDLancamento] = [Extent7].[IDLancamento]
                LEFT OUTER JOIN [fin].[LancamentoDocumento] AS [Extent8] ON [Extent5].[IDLancamento] = [Extent8].[IDLancamento]
                WHERE (0 = [Extent5].[Excluido]) AND ([Extent5].[IDConta] = @p__linq__0) AND (([Project2].[Descricao] = [Extent6].[Descricao]) OR (([Project2].[Descricao] IS NULL) AND ([Extent6].[Descricao] IS NULL))) AND ([Project2].[DataVencimento] = [Extent5].[DataVencimento]) AND ([Project2].[IDLancamento] = [Extent5].[IDLancamento]) AND ([Project2].[IDConta] = [Extent5].[IDConta]) AND ([Project2].[Valor] = [Extent5].[Valor])
            )  AS [Filter3]) AS [C1]
        FROM ( SELECT 
            [Distinct1].[IDLancamento] AS [IDLancamento], 
            [Distinct1].[IDConta] AS [IDConta], 
            [Distinct1].[Valor] AS [Valor], 
            [Distinct1].[DataVencimento] AS [DataVencimento], 
            [Distinct1].[Descricao] AS [Descricao]
            FROM ( SELECT DISTINCT 
                [Filter1].[IDLancamento2] AS [IDLancamento], 
                [Filter1].[IDConta] AS [IDConta], 
                [Filter1].[Valor2] AS [Valor], 
                [Filter1].[DataVencimento] AS [DataVencimento], 
                [Filter1].[Descricao] AS [Descricao]
                FROM   (SELECT [Extent1].[IDLancamento] AS [IDLancamento2], [Extent1].[IDConta] AS [IDConta], [Extent1].[Valor] AS [Valor2], [Extent1].[DataVencimento] AS [DataVencimento], [Extent2].[Descricao] AS [Descricao]
                    FROM   [fin].[Lancamento] AS [Extent1]
                    INNER JOIN [fin].[TipoLancamento] AS [Extent2] ON [Extent1].[IDTipoLancamento] = [Extent2].[IDTipoLancamento]
                    LEFT OUTER JOIN [fin].[ValorEfetuado] AS [Extent3] ON [Extent1].[IDLancamento] = [Extent3].[IDLancamento]
                    WHERE 0 = [Extent1].[Excluido] ) AS [Filter1]
                LEFT OUTER JOIN [fin].[LancamentoDocumento] AS [Extent4] ON [Filter1].[IDLancamento2] = [Extent4].[IDLancamento]
                WHERE [Filter1].[IDConta] = @p__linq__0
            )  AS [Distinct1]
        )  AS [Project2]
    )  AS [Project3]
)  AS [Project4]

两个查询的区别:

  • 第二个有一个新的左连接_ld
  • 第二个有 ... group new {_ve,_ld} by ... 而不是 ... group _ve by ...
  • 最后,我进行计数操作,这就是我想做的事情 QtdContaDocumento = grupo.Count(_ldcount => _ldcount._ld.IDLancamento != null)

我认为我误解了实体中的左连接或分组。让它正常工作并不难

4

0 回答 0