简体   繁体   中英

Linq's Column In (Select …) Statement

How do you write a linq statement that does the equivalent of this subselect in MS SQL:

... WHERE
tblXref.Organization_Id IN (SELECT Organization_Id
                   FROM AppUser au INNER JOIN [User] u ON au.User_Id = u.Id
                   WHERE u.Username = usernameVariable)

Well, it's probably simpler to write the inner query separately (remembering that you're not executing the query):

var innerQuery = from au in db.AppUsers
                 join u in db.Users on au.User_Id equals u.Id
                 where u.UserName == userNameVariable
                 select au.Organization_Id;

var query = from tblXref in db.CrossReferences // or whatever
            where innerQuery.Contains(tblXref.Organization_Id)
            ...;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM