问题
This must be simple, 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?
Tks
回答1:
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;
Easier still if you have a navigation/association from Table1 to Table2. But you didn't show that, so neither will I....
来源:https://stackoverflow.com/questions/4694212/linq-to-entities-subquery-in-the-where-statement