Linq to Entities - Subquery in the where statement

倖福魔咒の 提交于 2019-12-13 00:45:44

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!