Linq中的嵌套查询

时间:2012-06-14 12:42:22

标签: asp.net-mvc visual-studio-2010 linq

我有三张桌子。我必须使用Linq语句检索数据。我的三个表名是A,B,C。我连接连接以基于id的连接两个表A和B:

select ol, fN, LN, ci, co
from member
join details
on member_id = details_id
where details_id in
(select contacts_id from contacts where
contacts_id1 = 1 and contacts_usr_id = 1)

我能够将查询编写到where条件,如何编写查询内部条件?

2 个答案:

答案 0 :(得分:1)

你可以试试这个:

      var idlist = (from tbl in table3
                    where tbl.usr_id == 1 && tbl.contacts_id == 1
                    select tbl.contacts_id ).ToList();

     var x = from A in table1
              from B in table2 where A.user_id == B.user_id                
              && idlist.Contains(A.user_id)
              select new { a = A.a, b = A.b, c = A.c, d = B.d, e = B.e };

检查并告知我是否有这项工作。

答案 1 :(得分:0)

尝试颠倒翻转查询。如下:

var query = 
   from t3 in table3
   where t3.user_id = 1 && t3.contacts_id = 1
   join t2 in table2 on t3.contacts_id equals t2.usr_id
   join t1 in table1 on t2.usr_id equals t1.userid
   select new {t2.a, t2.b, t2.c, t1.d, t1.e};