Linq to Entities - where语句中的子查询

时间:2011-01-14 17:51:32

标签: entity-framework entity-framework-4 linq-to-entities

这一定很简单,但我一直在寻找2个小时,但找不到答案。我如何在Linq to Entities中写这个:

SELECT Reg4, Reg5, Reg6
FROM dbo.Table1
WHERE Reg1 = 15
AND Reg2 = ( SELECT MAX(Reg2) FROM dbo.Table2 WHERE Reg1 = 15);

是否可以在查询表达式和基于方法的语法中执行此操作?
TKS

1 个答案:

答案 0 :(得分:4)

var r1 = 15;
var q = from t in Context.Table1
        where t.Reg1 == r1 && 
              t.Reg2 == Context.Table2
                               .Where(t2 => t2.Reg1 == r1)
                               .Max(t2 => t2.Reg2)
        select t;

如果您有从Table1到Table2的导航/关联,则更容易。但是你没有表现出来,所以我也不会......