只需查看 linq 查询和 SQL 分析器生成的查询。Linq 重复它不应该的子查询。这是一个错误还是我做错了什么?顺便说一句,我知道如何编写这个 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]