使用过滤器子查询将Sql Query转换为DBContext查询

时间:2012-03-02 11:06:52

标签: sql filter subquery dbcontext

我有这个问题:

SELECT * FROM ScheduleGroups A
INNER JOIN ScheduleGroupResources B ON A.ScheduleGroupId=B.ScheduleGroupId
WHERE B.ScheduleGroupId IN (SELECT ScheduleGroupId 
                            FROM  ScheduleGroupResources 
                            WHERE ScheduleResourceId=2)

我想用DbContext查询表示。

目前:

return actualContext.ScheduleGroups
                    .Include(a => a.ScheduleGroupResources).ToList();

这给我带来了所有结果。我只是想在'where'中显示过滤。

非常感谢你的一切!

编辑:

我找到了解决方案:

return actualContext.ScheduleGroups
                    .Include(a => a.ScheduleGroupResources)
                    .Where(a => a.ScheduleGroupResources.Any(b => b.ScheduleResourceId == scheduleResourceId)).ToList();

1 个答案:

答案 0 :(得分:0)

仅供参考,原始查询可以写成:

SELECT * 
FROM ScheduleGroups A
INNER JOIN ScheduleGroupResources B ON A.ScheduleGroupId=B.ScheduleGroupId
WHERE ScheduleResourceId=2