简体   繁体   中英

SQL/ Linq query select from many to many

I have following (simplified) structure in my SQL database. 在此处输入图片说明

Now I would like to select all groups where the user is not yet part of. It would help if the query is written using Linq/Entity Framework but I can work with the SQL statement too.

I tried using Include (using EF):

.Include(g => g.Group_User) 

in the query and than using a:

.Where(g => g.Group_User.UserId != userId) 

but that did not work and is probably totally not the correct way of doing these kind of query's.

Thanks for reading

I would like to select all groups where the user is not yet part of.

So you want all groups without a group-user with this UserId , so use !Any and == :

var q = db.Group.Include(g => g.Group_User)
    .Where(g => !g.Group_User.Any(gu => gu.UserId == userId));

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