Linq Group by需要花费大量时间,而sql中的相同查询需要0.2秒

时间:2018-11-20 19:45:10

标签: c# mysql linq

我正在尝试联接4个表并将它们相应地分组并计算一个值。

var query = from sd in sdddd
                join ei in eiii.Where(ei1 => ei1.bi == 110) on sd.sdi equals ei.sdi
                join pch in pchhhh.Where(bpch =>  bpch.bi == 110) on  sd.sdi equals pch.sdi 
                join ass in baaaa.Where(pc => pc.bi == 110) on pch.bi equals ass.bi
            where ei.bi == 110
            group new { sd,ei,pch, ass} by new { sd.dis,sd.dn } into x
                select new bs
                {
                    val1 = x.key.dn,
                    val2 = x.key.dis,
                    val3 = x.key.sdi,
                    val4 = x.key.di,
                    val5 = x.Sum(a=>a.pch.ab == "A" ? (a.pch.ans * a.ass.prm) : (a.pch.sb == "B" ? (a.pch.ans * a.ass.pim) : 0))
                };

我发现的问题与按功能分组。如果我删除groupby,它会非常快速地返回数据,但是在使用groupby时会花费很多时间

select  sd.[sdi],sd.[dnum] as 'Dept #',sd.[desc],
format(sum(case when pch.[ab]='A' then pch.[ans]*ass.prm 
else pch.[ans]*.pim end),'N2')
FROM [database].[sdddd] as sd
JOIN [database].[eiii] as ei on ei.[sdi] = sd.[sdi]
join [database].[pchhh] as pch on pch.[sdi]=sd.[sdi]
join [database].[ass] as ass on ass.bi = pch.bi 
WHERE ei.bi=110
GROUP BY sd.[desc],sd.[dnum],sd.[sdi]
ORDER BY sd.[dnum]

可以正常工作的原始sql查询。我只是想将这个查询转换为联接三个不同表并将它们分组在一起并转换为linq

0 个答案:

没有答案