thegunner
thegunner

Reputation: 7163

Exclude certain results from linq

I'm using linq to retrieve some users from my repository. User entities have a many to many relationship with a group entity. I want to retrieve all users - except some that are in one group.

i.e. give me all users who are not in group **id*?

        var users = repo.GetAll().Where(o => o.IsDeleted == false);

How can I say, except the users that are in group ...4? or group "name".

.Where(o => o.IsDeleted == false && !o.groups ?? );

Any ideas?

Upvotes: 1

Views: 824

Answers (1)

mihkov
mihkov

Reputation: 1189

var users = userRepo.GetAll()
    .Where(u => u.IsDeleted == false && u.Groups.All(g => g.Id != 4));

You should have a navigation property in the User class points to Group.

Upvotes: 2

Related Questions