1

我有一种方法,它使用 .NET 成员资格提供程序来使用配置文件提供程序为每个用户检索一些数据。但是我在我的应用程序日志中收到一个错误,指出

方法中GetUsers():的索引超出了数组的范围。

“in 方法GetUsers()”是由我的日志记录机制添加的。其余的是严格的.NET。

protected Dictionary<string, string> Getusers()
{
    try
    {
        Dictionary<string, string> userDictionary = new Dictionary<string, string>();

        //Get string array of users in role from RoleProvider
        var users = Roles.GetUsersInRole("MyRoleName");

        //loop through the array of usernames
        foreach (string user in users)
        {
            //Get user's first name and last name from the profile provider
            ProfileCommon profile = (ProfileCommon)ProfileCommon.Create(user);

            //add new key value pair with user name as the key and lastname,firstname as value
            userDictionary.Add(user, string.Concat(profile.LastName + ",", profile.FirstName));
        }

        //sort the dictionary by the value not the key
        //a little linq to do the sorting
        var sortedDict = (from entry in userDictionary orderby entry.Value ascending select entry).ToDictionary(pair => pair.Key, pair => pair.Value);

        return sortedDict;
    }
    catch (Exception ex)
    {
        LogController.LogError( "In Method -- Getusers() --" + ex.Message);
        //if error occurs return empty dictionary
        return new Dictionary<string, string>();
    }
}   
4

1 回答 1

0

你确定你在 userDictionary 中有值吗?

我想知道这是否是您看到异常的代码行

var sortedDict = (from entry in userDictionary orderby entry.Value ascending select entry).ToDictionary(pair => pair.Key, pair => pair.Value);

如果您发布堆栈跟踪,它将更容易知道。

于 2012-07-11T19:30:10.210 回答