Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
99 views
in Technique[技术] by (71.8m points)

c# - How to write this LIST in a CLASS instead of Code-Behind?

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;
    }
}

enter image description here

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...