开发者

Linq to Entities - Subquery in the where statement

开发者 https://www.devze.com 2023-02-05 05:25 出处:网络
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:

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?

Tks


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....

0

精彩评论

暂无评论...
验证码 换一张
取 消