Gilberto Quintero
Gilberto Quintero

Reputation: 397

Get elements of list that contains element of other list LINQ

I have to list created from database using entity framework LINQ queries

class Users {
string id { get; set; }
} 

class Permission
{
string permission id { get; set;}
User theUser { get; set; }
}

var allUsers = db.Users.ToList();
// This returns a list of ids ["1","2","3"]

var usersWithPermission = db.Permission.Where(x => x.User.Id.Contains(users));

I want to retrieve the users with permission, I am trying to get all the records from the permission table and then retrieve any of them that contains the id the id of the "AllUsers" list.

Thanks, Any help will be appreciate it

Upvotes: 0

Views: 399

Answers (2)

Kamran Khan
Kamran Khan

Reputation: 1109

you may try this

var usersWithPermission = db.Permission.Where(x => allUsers.Contains(x.User.Id));

Upvotes: 2

har07
har07

Reputation: 89325

You can use Any() to accomplish this :

var usersWithPermission = db.Permission
                            .Where(x => allUsers.Any(u => x.User.Id == u.Id))
                            .ToList();

Upvotes: 3

Related Questions