2014-01-20 175 views
0

如何將下列SQL語句轉換爲linQ將sql語句轉換爲linq MVC

select * from student where student_id in (4 , 10 , 20 , 50) 

我試過以下,但它不工作:

var SelectedStudent = db.Students 
      .Include(i => i.grade) 
      .Where(i => i.studentID in (1,2,3)) 
      .ToList(); 

回答

1
var ids = new [] { 1,2,3}; 

var students = 
    from student in db.Students 
    where ids.Contains (student.StudentId) 
    select student; 
0
var collIds={10,20,30,40}; 

var results=(from s in dbo.Student 
     join c in collIds 
     on s.id equals c 
     select s).ToList();