繁体   English   中英

将用户的ICollection列表添加到C#中的现有角色列表中

[英]Add ICollection list of User to Existing Roles List in C#

我有User和Role类,每个类都有List定义。 现在,我想将用户列表添加到特定角色,换句话说,我想将用户参考列表放入ICollection Role中。

用户

 public class User
{
    public int UserID { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }
    public  bool ActiveStatus { get; set; }

     public List<User> Users = new List<User>();

  public List<User> BuildUserRepository()
    {
        Users.Add(new User { UserID = 01, Name = "Khurram", Address = "London", ActiveStatus = true });
        Users.Add(new User { UserID = 02, Name = "Sana", Address = "London", ActiveStatus = true });
        Users.Add(new User { UserID = 03, Name = "Richard", Address = "London", ActiveStatus = false });
        Users.Add(new User { UserID = 04, Name = "Tracy", Address = "London", ActiveStatus = true });
        Users.Add(new User { UserID = 05, Name = "Laura", Address = "Manchester", ActiveStatus = true });
        Users.Add(new User { UserID = 06, Name = "James", Address = "London", ActiveStatus = false });

        return Users;
    }
}

角色

public class Role
{
    public int RoleID { get; set; }
    public string RoleTitle { get; set; }
    public ICollection<User> UsersInRole { get; set; }

    public List<Role> Roles = new List<Role>();

    public void BuildRoleRepository()
    {
        Roles.Add(new Role { RoleID = 01, RoleTitle = "Admin" });
        Roles.Add(new Role { RoleID = 02, RoleTitle = "Management" });
        Roles.Add(new Role { RoleID = 03, RoleTitle = "User" });
    }
 }

现在我想将例如UserID 01和02的两个用户添加到Role ICollection,其中RoleID = 01,我该怎么做

无需对解决方案的设计进行任何大的改动,就可以通过下面的以下代码片段实现它。 但是请记住,对类内部的实际/测试数据进行硬编码通常会进一步引入很多限制,并且通常被认为是一种反模式。 使用外部数据存储(文件或数据库)和存储库模式作为持久层可能会更好。 希望能帮助到你!

Role.Roles.First(x => x.RoleID == 01)
    .UsersInRole.AddRange(
        User.Users.Where(x => x.UserID == 01 || x.UserID == 02));

public class User
{
    public int UserID { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }
    public bool ActiveStatus { get; set; }

    public static List<User> Users = new List<User>
    {
        new User {UserID = 01, Name = "Khurram", Address = "London", ActiveStatus = true},
        new User {UserID = 02, Name = "Sana", Address = "London", ActiveStatus = true},
        new User {UserID = 03, Name = "Richard", Address = "London", ActiveStatus = false},
        new User {UserID = 04, Name = "Tracy", Address = "London", ActiveStatus = true},
        new User {UserID = 05, Name = "Laura", Address = "Manchester", ActiveStatus = true},
        new User {UserID = 06, Name = "James", Address = "London", ActiveStatus = false}
    };
}

public class Role
{
    public int RoleID { get; set; }
    public string RoleTitle { get; set; }
    public List<User> UsersInRole { get; set; }

    public static List<Role> Roles = new List<Role>
    {
        new Role {RoleID = 01, RoleTitle = "Admin"},
        new Role {RoleID = 02, RoleTitle = "Management"},
        new Role {RoleID = 03, RoleTitle = "User"}
    };
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM