[英]Fill a Multi-Dimensional Array
I have something like this hard-coded: 我有这样的硬编码的东西:
private string[,] m_RolesForUser = new string[,] {
{"John","President,Chair"},
{"Lisa","Chair"},
{"Mike","Executive,President,Chair"},
};
How would I fill this array if the datasource was made up of a table of roles and a table of users. 如果数据源由角色表和用户表组成,我将如何填充该数组。 A user could have multiple roles.
一个用户可以具有多个角色。 Not sure what the syntax is to construct the code to support something like this.
不确定构造该代码以支持类似内容的语法是什么。
Why not use a Dictionary
of a list of objects here? 为什么不在此处使用包含对象列表的
Dictionary
? C# is an OO language, so using objects is much more preferred. C#是一种OO语言,因此更喜欢使用对象。 The example below is using strings to fit your example, but you could even create a
Person
class and a Role
class and have a Person
tied to a list of Role
s 下面的示例使用字符串适合您的示例,但是您甚至可以创建一个
Person
类和一个Role
类,并使Person
绑定到Role
的列表
private Dictionary<string, List<string>> roles = new Dictionary
{
{"John", new List{"President","Chair"}},
{"Lisa", new List{"Chair"}},
{"Mike", new List{"Executive","President","Chair"}}
}
To look for Lisa's roles: 要查找Lisa的角色,请执行以下操作:
//Just verifying that Lisa exists.
//If you try to access a non-existent key you will get an exception
var roleLookingFor = "Lisa";
if(roles.Contains(roleLookingFor))
{
foreach(var role in roles[roleLookingFor])
{
Console.Out.WriteLine(String.Format("{0} is in role '{1}'.",
roleLookingFor, role));
}
}
You have different options for data structures. 您对数据结构有不同的选择。 One Idea would be to have a class for the users and to store the roles as a list for each user
一个想法是为用户提供一个类,并将角色存储为每个用户的列表
public class User
{
public User ()
{
Roles = new List<string>();
}
public int ID { get; set; }
public string Name { get; set; }
public List<string> Roles { get; private set; }
}
Then you can have a list of users 然后,您可以有一个用户列表
List<User> _users = new List<User>();
If you want to store more information for each role, you could have a Role class as well having a user list for each role. 如果要为每个角色存储更多信息,则可以具有一个Role类以及每个角色的用户列表。
The advantage of List<T>
versus arrays is that lists grow dynamically. List<T>
与数组相比的优势在于列表可以动态增长。
Fill this structure like this 像这样填充这个结构
using (OleDbConnection cnn = new OleDbConnection(ConnectionString)) {
string query = "SELECT ... FROM users LEFT JOIN user_roles ON ... ORDER BY UserID";
using (OleDbCommand cmd = new OleDbCommand(query, cnn)) {
cnn.Open();
using (OleDbDataReader reader = cmd.ExecuteReader()) {
int userIdOrdinal = reader.GetOrdinal("UserID");
int userNameOrdinal = reader.GetOrdinal("UserName");
int roleIdOrdinal = reader.GetOrdinal("RoleID");
int roleNameOrdinal = reader.GetOrdinal("RoleName");
User user = null;
while (reader.Read()) {
int userID = reader.GetInt32(userIdOrdinal);
if (user == null || user.ID != userID) {
user = new User { ID = userID };
user.Name = reader.GetString(userNameOrdinal);
_users.Add(user);
}
if (!reader.IsDBNull(roleIdOrdinal)) {
user.Roles.Add(reader.GetString(roleNameOrdinal);
}
}
}
}
}
(Of cause you would use an approriate connection type, reader type, etc.) (因此,您将使用适当的连接类型,读取器类型等)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.