5

我尝试在实体框架中实现这个 SQL 查询

select * 
from students 
where student.fieldid in (select fieldid from fields where groupid = 10)

我想这是一种方法:

var fields = context.fields.where(t=>t.groupid = 10).toList();

var result = context.students.where(t=> fields.Contains(t.fieldid)).toList();

但这不起作用!

有没有其他人试图做这样的事情?

4

1 回答 1

6

A SQL IN is equivalent to a LINQ Contains.

var names = new string[] { "Alex", "Colin", "Danny", "Diego" };    
var matches = from person in people
              where names.Contains(person.Firstname)
              select person;

So, the SQL statement:

select * from students where student.fieldid in ( select fieldid from fields 
where groupid = 10)

is Equivalent in LINQ to:

var fieldIDs= from  Fids in db.fields
              where Fids.groupid==10
              select Fids.fieldid;

var results= from s in db.students
             where fieldIDs.Contains(s.fieldid)
             select s;
于 2013-07-28T07:49:18.257 回答