将用户的 ICollection 列表添加到 C# 中的现有角色列表
Add ICollection list of User to Existing Roles List in C#
我有用户和角色类,每个都有列表定义。现在我想将用户列表添加到特定角色,换句话说,我想将用户引用列表放在 ICollection 角色中。
用户
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 的两个用户添加到角色 ICollection,其中 RoleID = 01,我该怎么做
如果不对您的解决方案的设计进行一些重大更改,您可以通过下面的代码片段来完成它。但请记住 hard coding of the actual/testing data inside your classes is usually introducing a lot of limitations further on and is often considered an anti-pattern. You might be better off using external data storage (file or database) and Repository Pattern 作为您的持久层。希望对您有所帮助!
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"}
};
}
我有用户和角色类,每个都有列表定义。现在我想将用户列表添加到特定角色,换句话说,我想将用户引用列表放在 ICollection 角色中。
用户
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 的两个用户添加到角色 ICollection,其中 RoleID = 01,我该怎么做
如果不对您的解决方案的设计进行一些重大更改,您可以通过下面的代码片段来完成它。但请记住 hard coding of the actual/testing data inside your classes is usually introducing a lot of limitations further on and is often considered an anti-pattern. You might be better off using external data storage (file or database) and Repository Pattern 作为您的持久层。希望对您有所帮助!
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"}
};
}