2017-08-11 96 views
0

我想通過使用linq編寫左連接,但它給出了一個錯誤。這是SQL代碼:如何使用左連接linq

 Select 
      Case s.StudentID 
      When 1000 Then s.Amount 
      End As Amount 
     From Student s 
     Left Join Course cOn c.Time = s.ForDate And c.StudentID= s.ID And s.Amount= 1000 

這一個是我試過的代碼,但它給出了一個錯誤的左連接:

 from s in Student 
     join c in Course 
              on new {s.ForDate , s.ID} 
              equals new { c.Time, c.StudentID}          
              into inj 

              from i in inj.DefaultIfEmpty() 
              where s.Amount= 1000 

回答

0

試試這個

from s in Student 
     join c in Course on new {A=s.ForDate ,B= s.ID} 
          equals new {A= c.Time,B= c.StudentID}          
          into inj 
          from i in inj.DefaultIfEmpty() 
          where s.Amount == 1000 
          select new{ 
          /// 
          } 
+0

確保s.ForDate和c.Time是同一類型的,或者你需要使用強制 – Rise

+0

它給出錯誤的s.Amount = 1000。我也試過inj.Amount但仍然給出錯誤 – phi

+0

我錯過了where子句中的=運算符。再次檢查它 – Rise

0

試試這個

from s in Student.Where(p=>p.Amount == 1000) 
join c in Course on new {s.ForDate , s.ID} equals new { c.Time, c.StudentID} into inj from i in inj.DefaultIfEmpty() 
select new{ 
} 
0

你的SQL非常奇特,似乎沒有太多的東西有用的,但翻譯是:

from s in Student 
where s.Amount == 1000 
select (s.StudentID == 1000 ? s.Amount : null)