2015-10-30 170 views
0

如何將以下查詢轉換爲lambda表達式或LINQ?如何將此sql請求轉換爲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

很難回答沒有實體模型(涉及3個實體及其關係)。 –

回答

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() 
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... 
      } 
+0

我認爲我們可以通過提供完整的查詢來改進答案。 –

+1

第三行產生「CS0103 - 名稱'ci'在當前上下文中不存在' –

+0

你說的對,我寫到快速 – RomainV

相關問題