1

2 つの左結合を作成し、これら 2 つの左結合からデータを合計/カウントする必要があります。

最初に、左結合 (ValorEfetuado) を 1 つだけ試しました。このテーブルにはデータがある可能性があり、データがある場合は合計する必要があります。

それは、1つの左結合とそれが生成する選択を含む元のクエリです(かなりシンプルだと思います)

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]

これで、group by と sum ができました。今問題はです。新しい左結合をクエリに入れ、それによってグループ化すると、機能しないデーモンクエリが得られました。大きい。そして非効率です。この例のように

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)
                    };

これは、私が求めたものを単純に返さない1つの醜い巨大なクエリになります

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]

2 つのクエリの違い:

  • 2 番目のものには、新しい左結合 _ld があります。
  • 2 つ目は ... group new {_ve,_ld} by ... の代わりに ... group _ve by ...
  • 最後に、私がやりたいことであるカウント操作を行います QtdContaDocumento = grupo.Count(_ldcount => _ldcount._ld.IDLancamento != null)

エンティティの左結合またはグループ化を誤解していると思います。それを正しく機能させるのはそれほど難しいことではありません

4

0 に答える 0