I have this working method in code-behind which gets users & their roles:
//code-behind working fine
private void DisplayUserAndRolesInGrid()
{
using (myDB dataContext = new myDB())
{
var UserRole = (from u in dataContext.aspnet_Users.Include("aspnet_Roles")
from r in u.aspnet_Roles
where r != null
select new { User = u, Role = r }).ToList();
grdUserRoles.DataSource = UserRole.ToArray();
grdUserRoles.DataBind();
}
}
But I need to place it in a class & return a List, so I did following which throws below error (I guess I need to somehow modify this line: public List<aspnet_Roles> GetUserAndRoles()):
//in a Class with error
public class UsersClass
{
public List<aspnet_Roles> GetUserAndRoles()
{
List<aspnet_Roles> UserRoles = new List<aspnet_Roles>();
using (myDB dataContext = new myDB())
{
UserRoles = (from u in dataContext.aspnet_Users.Include("aspnet_Roles")
from r in u.aspnet_Roles
where r != null
select new { User = u, Role = r }).ToList();
}
return UserRoles;
}
}
Any help is appreciated. Please I'm stuck on this.
question from:
https://stackoverflow.com/questions/65599769/how-to-write-this-list-in-a-class-instead-of-code-behind 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…