0

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

2
  • 1
    You could try something like: ..Permission.Where (x => allUsers.Contains (x.User.Id)) Commented Apr 4, 2016 at 5:33
  • Try make class and properties public.
    – jdweng
    Commented Apr 4, 2016 at 5:37

2 Answers 2

3

You can use Any() to accomplish this :

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

you may try this

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.