简体   繁体   中英

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? C# is an OO language, so using objects is much more preferred. 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

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:

//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.

The advantage of List<T> versus arrays is that lists grow dynamically.

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.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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