简体   繁体   中英

L2E many to many query

I have four tables:

Users                  PrivilegeGroups        rdPrivileges        LinkPrivilege
-----------            ----------------       ---------------     ---------------
userId(pk)             privilegeGroupId(pk)   privilegeId(pk)     privilegeId(pk, fk)
privilegeGroupId(fk)   name                   code                privilegeGroupId(pk, fk)

L2E will not create LinkPrivilege entity for me. So we only have Users , PrivilegeGroups and rdPrivileges entities. PrivilegeGroups and rdPrivileges are many to many relationship.

What I need to do is retrieve all code from rdPrivileges table based on a passed in userId. How can I do it?

EDIT

credite to juharr, the working code:

var codes = from u in db.Users
          from pg in db.PrivilegeGroups
          from p in pg.rdPrivileges
          where u.UserId == passedInUserId
             && u.PrivilegeGroups.PrivilegeGroupId == pg.PrivilegeGroupId
          select p.Code;

I think something like this would work.

var codes = from u in Users
            from pg in u.PrivilegeGroups
            from p in pg.rdPrivileges
            where u.userId == "SomeUserID"
            select p.code;

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