2017-08-11 82 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)