steve
steve

Reputation: 664

Nested query in Linq

I have three tables. I have to retrieve the data using Linq statement. My three table names are A,B,C. I connected join for connecting two tables A and B based on the id's like:

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)

I am able to write the query up to the where condition, how can I write the query for the inner while condition?

Upvotes: 0

Views: 836

Answers (2)

Jim Wooley
Jim Wooley

Reputation: 10418

Try flipping the query upside down. How about the following:

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};

Upvotes: 0

Behnam Esmaili
Behnam Esmaili

Reputation: 5967

you can try this:

      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 };

check and let me know if that work.

Upvotes: 1

Related Questions