【问题标题】:Implement in operator in Entity Framework在实体框架中实现 in 运算符
【发布时间】:2013-07-28 07:13:19
【问题描述】:

我尝试在实体框架中实现这个 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();

但这不起作用!

有没有其他人尝试过这样的事情?

【问题讨论】:

    标签: linq entity-framework extension-methods


    【解决方案1】:

    SQL IN 等效于 LINQ Contains

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

    所以,SQL 语句:

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

    在 LINQ 中等价于:

    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;
    

    【讨论】:

      猜你喜欢
      • 2015-09-27
      • 1970-01-01
      • 2010-11-05
      • 1970-01-01
      • 2011-12-16
      • 1970-01-01
      • 2011-09-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多