如何将Sql查询转换为包含子查询的Linq

时间:2013-05-08 13:20:02

标签: c# sql linq subquery

我的查询跟随

   `Select * from daps_user_activity where Userid In (Select Userid from daps_portaluser  where EMR_ID = 24855) `

linq中的等效查询请帮助我......

2 个答案:

答案 0 :(得分:0)

试试这个,最好在这个实例中使用join而不是子查询:

var results = (from a in daps_user_activity
              join u in daps_portaluser on a.Userid equals u.Userid
              where u.EMR_ID == 24855
              select a).ToList()

答案 1 :(得分:0)

或者,你可以使用它:

var results = (from a in daps_user_activity
               from u in daps_portaluser
               where u.EMR_ID == 24855 
               && a.Userid == u.Userid
               select a).ToList()

对我而言,它更清楚地显示了主查询和子查询。

归功于Convert SQL Query (with Correlated Subquery) to LINQ in C#

的@Bruno Brant