Linq查询有错误吗?

时间:2018-05-28 14:33:28

标签: c# sql-server linq entity-framework-6

只需查看linq查询和SQL事件探查器生成的查询。 Linq重复了它不应该的子查询。这是一个错误还是我做错了什么?顺便说一句,我知道怎么写这个linq给我一个令人满意的结果,但我想知道为什么这个被严重解释?

        return (from k in ctx.UzajamnaKasa
                join p in ctx.UzajamnaKasaPozajmice
                    on k.UzajamnaKasaId equals p.UzajamnaKasaId
                where k.ClanId == clanId
                select p.Iznos + p.Donos -
                    (from ot in ctx.UzajamnaKasaPozajmiceOtplate
                     where p.PozajmicaId == ot.PozajmicaId
                     group ot.Iznos by ot.PozajmicaId into g
                     select g.Sum()).FirstOrDefault()
                ).Sum();



select [GroupBy3].[A1] as [C1]
from
(
    select sum([Project2].[A1]) as [A1]
    from
    (
        select ([Project2].[Iznos] + [Project2].[Donos])
               - (case
                      when ([Project2].[C1] is null) then
                          cast(0 as decimal(18))
                      else
                  (
                      select top (1)
                             [GroupBy2].[A1] as [C1]
                      from
                      (
                          select [Extent4].[PozajmicaId] as [K1]
                               , sum([Extent4].[Iznos])  as [A1]
                          from [dbo].[UzajamnaKasaPozajmiceOtplate] as [Extent4]
                          where [Project2].[PozajmicaId] = [Extent4].[PozajmicaId]
                          group by [Extent4].[PozajmicaId]
                      ) as [GroupBy2]
                  )
                  end
                 ) as [A1]
        from
        (
            select [Extent2].[PozajmicaId] as [PozajmicaId]
                 , [Extent2].[Iznos]       as [Iznos]
                 , [Extent2].[Donos]       as [Donos]
                 , (
                       select top (1)
                              [GroupBy1].[A1] as [C1]
                       from
                       (
                           select [Extent3].[PozajmicaId] as [K1]
                                , sum([Extent3].[Iznos])  as [A1]
                           from [dbo].[UzajamnaKasaPozajmiceOtplate] as [Extent3]
                           where [Extent2].[PozajmicaId] = [Extent3].[PozajmicaId]
                           group by [Extent3].[PozajmicaId]
                       ) as [GroupBy1]
                   )                       as [C1]
            from [dbo].[UzajamnaKasa]                    as [Extent1]
                inner join [dbo].[UzajamnaKasaPozajmice] as [Extent2]
                    on [Extent1].[UzajamnaKasaId] = [Extent2].[UzajamnaKasaId]
            where [Extent1].[ClanId] = 1303
        ) as [Project2]
    ) as [Project2]
) as [GroupBy3];

为什么他会复制这个选择?这对于表演来说很糟糕。:

 select top (1)
                             [GroupBy2].[A1] as [C1]
                      from
                      (
                          select [Extent4].[PozajmicaId] as [K1]
                               , sum([Extent4].[Iznos])  as [A1]
                          from [dbo].[UzajamnaKasaPozajmiceOtplate] as [Extent4]
                          where [Project2].[PozajmicaId] = [Extent4].[PozajmicaId]
                          group by [Extent4].[PozajmicaId]
                      ) as [GroupBy2]

1 个答案:

答案 0 :(得分:0)

                    (from ot in ctx.UzajamnaKasaPozajmiceOtplate
                 where p.PozajmicaId == ot.PozajmicaId
                 group ot.Iznos by ot.PozajmicaId into g
                 select g.Sum()).FirstOrDefault()

由于FirstOrDefault(),将为每个创建的p.PozajmicaId执行此子查询。