开发者

Is looping through the entityreference the correct way?

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 开发者_JS百科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();
        }


Is it possible that your Role table has a Users property? I would think that it would name the navigation property Users, not UsersReference. I don't use EF, but all the examples I've seen name the property after the table. AFAIK it always implements IEnumerable so you should be able use it in a foreach.

If you have it set up right I think you only need:

using (var context = new MyEntities())
{
    return context.Roles
                  .Where( r => r.RoleName == roleName )
                  .SelectMany( r => r.Users )
                  .Select( u => u.UserName )
                  .ToArray();
}


Try using your original foreach loop with ToList()

foreach(var user in role.Users.ToList()) {...}


Use the .ToArray() helper instead

using (var context = new MyEntities())
{
    return (from role in context.Roles
            where role.RoleName == roleName
            from user in r.Users
            select user.UserName).ToArray();
}

Note that there's no need for .Include("Users") if you do it this way. Using r.Users in the query causes it to be in one query without the need for including it, because it's used in an active ObjectContext.

One a side note, I'm not sure what the method signature of this method is, but in this case IEnumerable<string> is probably a better fit than string[], because you can adjust the implementation later without creating arrays from other types of collections.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜