Linq to Entities - Subquery in the where statement
This must be simple, 开发者_JS百科but I've been searching for 2 hours, and can't find an answer. How do I write this in Linq to Entities:
SELECT Reg4, Reg5, Reg6
FROM dbo.Table1
WHERE Reg1 = 15
AND Reg2 = ( SELECT MAX(Reg2) FROM dbo.Table2 WHERE Reg1 = 15);
Is it possible to do it both in query expressions and method based syntaxes?
Tksvar 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;
Easier still if you have a navigation/association from Table1 to Table2. But you didn't show that, so neither will I....
精彩评论