Reputation: 89
I have a many to many relationship between Users and Roles as a user can have many roles and a role can be associated to many users.
As role titles I have guest (id 1) and normalUser(id 2)
I am trying to get all the normalUser that there are in the database but I do not want the guests because i have to set permissions as required
this is my code so far
public List<User> GetnormalUsers()
{
return Entity.Users.Where(x => x.Roles.Contains(2).ToList());
}
this is giving me an error stating that I cannot convert from 'int' to 'Common.Role'.
How should I fix this?
Upvotes: 2
Views: 444
Reputation: 45091
From the given partial data I would try
return Entity.Users.Where(user => user.Roles.Any(role => role.Id == 2)).ToList();
Two problems where in your statement:
.ToList()
statement on the wrong position, materializing the wrong collection..Contains()
doesn't work that way. Instead you have to use .Any()
.Upvotes: 7
Reputation: 792
Two things: you need to access the Id (presumably) property of Roles in order for your filter to work, and your .ToList() is in the wrong position.
public List<User> GetnormalUsers()
{
return Entity.Users.Where(x => x.Roles.Any(y => y.Id == 2)).ToList();
}
Upvotes: 3