many to many Entity Framework GetUsersInRole()

wayneshamrock

New Member
I have two Entity types: Role and User that have many to many relation. That is Role has the Users property, and User has the Roles property. I must get the string array with usernames that belong to defined role.I wrote some code, but it is not elegant. I want do that with one linq expression. Is it possible?\[code\]public override string[] GetUsersInRole(string roleName){ List<string> names = new List<string>(); using (MembershipDb db = new MembershipDb()) { Role role = db.Roles.FirstOrDefault(r => r.Name == roleName); foreach (User u in role.Users) { names.Add(u.UserName); } } return names.ToArray();}\[/code\]
 
Back
Top