我想将以下SQL查询转换为linq查询,但我感到困惑:
SELECT *
FROM dbo.Vahed
INNER JOIN dbo.VahedMahsol ON dbo.Vahed.VahedId = dbo.VahedMahsol.VahedId
RIGHT OUTER JOIN dbo.Mahsol ON dbo.VahedMahsol.MahsolId = dbo.Mahsol.MahsolId
我写了这段代码:
vaheds =
(
from i in db.spGetVahedByWhatWhere(what, wherestr, int.Parse(whattype), new Guid(shahrid))
join gr in db.GorohSenfis on i.GorohSenfiId equals gr.GorohSenfiID
join ct in db.Contacts on i.VahedId equals ct.VahedId
join vm in db.VahedMahsols on i.VahedId equals vm.VahedId
select i
)
.ToList();
但我不知道如何转换
RIGHT OUTER JOIN dbo.Mahsol ON dbo.VahedMahsol.MahsolId = dbo.Mahsol.MahsolId
到linq查询。
答案 0 :(得分:3)
首先,您可以重写SQL查询以使用LEFT OUTER JOIN而不是RIGHT OUTER JOIN(因为这更容易转换为LINQ):
SELECT *
FROM dbo.Mahsol
LEFT OUTER JOIN dbo.VahedMahsol ON dbo.Mahsol.MahsolId = dbo.VahedMahsol.MahsolId
INNER JOIN dbo.Vahed ON dbo.VahedMahsol.VahedId = dbo.Vahed.VahedId
现在您可以在LINQ中转换上面的查询,如下所示:
vaheds = (
from m in db.Mahsols
join vm1 in db.VahedMahsols on m.MahsolId equals v.MahsolId into vmgroup
from vm in vmgroup.DefaultIfEmpty()
join i in db.spGetVahedByWhatWhere(what, wherestr, int.Parse(whattype), new Guid(shahrid))
on vm.VahedId equals i.VahedId
join gr in db.GorohSenfis on i.GorohSenfiId equals gr.GorohSenfiID
join ct in db.Contacts on i.VahedId equals ct.VahedId
select i
).ToList();
我无法对此进行测试,但我希望它有效。