如何在实体框架.net中做子查询

时间:2014-11-20 07:10:37

标签: asp.net .net entity-framework subquery

以下是我尝试转换为实体框架的查询示例

 select * from Teachers fm 
    where (select count(*) from General_Program
    where Teacher_id = fm.Teacher_ID and Campus_Name = fm.Campus_Name)  > 0 
    and Dept_Name = 'English'

1 个答案:

答案 0 :(得分:1)

// Without relation between Teachers and General_Program:    
var teachers = _context.Teachers.Where(t => t.Dept_Name = "English" && _context.General_Program.Any(p => p.Campus_Name = t.Campus_Name && p.Teacher_id = t.Teacher_ID));

// When Teachers are related to General_Program (only on ID):
var teachers = _context.Teachers.Where(t => t.Dept_Name = "English" && t.General_Program.Any(p => p.Campus_Name = t.Campus_Name));

// In case of Teachers are related to General_Program (key contains both ID and campus):
var teachers = _context.Teachers.Where(t => t.Dept_Name = "English" && t.General_Program.Any());