Is looping through the entityreference the correct way?
Posted
by Jova
on Stack Overflow
See other posts from Stack Overflow
or by Jova
Published on 2010-03-21T13:20:25Z
Indexed on
2010/03/21
13:31 UTC
Read the original article
Hit count: 340
I want to get all users that have a specific role to a list of usernames.
Is using .Include to include all the users, and going through the UsersReference the best way to loop through all the users that are associated with the role?
I noticed I could not do a foreach(User user in role.Users) but UsersReference seem to work, but is that how it's supposed to be done? Going through the reference?
using (var context = new MyEntities())
{
List<string> users = new List<string>();
Role role = (from r in context.Roles.Include("Users")
where r.RoleName == roleName
select r).FirstOrDefault();
foreach (User user in role.UsersReference)
users.Add(user.UserName);
return users.ToArray();
}
© Stack Overflow or respective owner