如何将以下查询转换为lambda表达式或LINQ?
SELECT * FROM PrelevementClient as pc
LEFT JOIN ContratIBAN AS ci ON ci.IDCMPT = pc.IDCMPT
LEFT JOIN Contrat AS ctr ON ctr.NOCONTRAT = ci.NOCONTRAT
WHERE pc.IDPRELEVEMENT = '111'
AND DTPROCHAINPREL >= getdate()
困难在于有两个左连接。
答案 0 :(得分:0)
希望有所帮助:
var query = from pc in Context.PrelevementClient
join ci in Context.ContratIBAN on pc.IDCMPT equals ci.IDCMPT into ciLeft
from ci in ciLeft.DefaultIfEmpty()
join co in Context.Contrat on ci.nocontrat equals co.nocontrat into coLeft
from co in coLeft.DefaultIfEmpty()
where pc.idprelevement == "111" && pc.dtprochainprel >= DateTime.Today
select new
{
//bind what you want
// and dont forget to manage if co||ci.property is null...
}
答案 1 :(得分:0)
每次“加入”后都会有一个“来自”。
在请求结束时对所有'from'进行分组会产生问题。
DateTime date = DateTime.Now.AddMonths(1);
var prelevements = from pc in set.Where(s => s.DTPROCHAINPREL >= DateTime.Today && s.NOSOC == nosoc && s.DTPROCHAINPREL < date)
join ci in context.Set<ContratIBAN>().Where( g => g.PRELEVEMENTBCA == true) on pc.IDCMPT equals ci.IDCMPT into cileft
from ci in cileft.DefaultIfEmpty()
join co in context.Set<Contrat>() on ci.NOCONTRAT equals co.NOCONTRAT into coleft
from co in coleft.DefaultIfEmpty()