I have the following bits of code in my accountRepository
public string[] GetRolesForUser(string email)
{
// User rolesUser = FindByMail(email);
IEnumerable<UserRole> RoleList = context.UserRolesSet.Where(u => u.user_id == 1).AsEnumerable();
string[] arr1 = new string[RoleList.Count()];
int i = 0;
foreach (UserRole r in RoleList)
{
arr1[i] = r.roles.name;
i++;
}
return arr1;
}
This should work but it doesn't. When it looping through the foreach loop it throws me this error:
开发者_如何学编程Exception Details: MySql.Data.MySqlClient.MySqlException: There is already an open DataReader associated with this Connection which must be closed first.
Is my foreach loop wrong?
You could simplify the method:
IEnumerable<UserRole> RoleList = context.UserRolesSet.Where(u => u.user_id == 1);
return RoleList.Select(x => x.roles.name).ToArray();
Try the following code. Using ToArray makes sure it populates all of the UserRoles before hand so it'll be finished with the DataReader.
public string[] GetRolesForUser(string email)
{
// User rolesUser = FindByMail(email);
IEnumerable<UserRole> RoleList = context.UserRolesSet.Where(u => u.user_id == 1).ToArray();
string[] arr1 = new string[RoleList.Count()];
int i = 0;
foreach (UserRole r in RoleList)
{
arr1[i] = r.roles.name;
i++;
}
return arr1;
}
精彩评论