SortedList<string, systemuser> Users = new SortedList<string, systemuser>();
Users.Add("username1",customobject);
Users.Add("username2",customobject);
Users.Add("username3",customobject);我要做的是在列表中搜索一个特定的键(即用户名),然后返回列表中的下一个条目。如果我在列表的末尾,那么我需要在第一个位置返回项目。
不知道如何实现这一点,因为SortedList似乎没有公开索引属性。
我有:
foreach (KeyValuePair<string, systemuser> pair in Users)
{
if (pair.Key == lastAllocation)
{
// RETURN THE ITEM IMMEDIATELY AFTER THIS ONE
}
}任何建议都要感谢!
发布于 2015-05-14 23:00:16
您是在寻找下一个项目(KeyValuePair)、值还是索引?
bool returnNextItem = false;
foreach (KeyValuePair<string, systemuser> pair in Users)
{
if (returnNextItem)
return pair.Value;
if (pair.Key == lastAllocation)
returnNextItem = true;
}发布于 2015-05-14 23:00:20
首先获取搜索元素的索引。然后获取所需的密钥(不要忘记模块%)。最后访问所爱的人:)
SortedList<string, systemuser> Users = new SortedList<string, systemuser>();
Users.Add("username1",customobject);
Users.Add("username2",customobject);
Users.Add("username3",customobject);
var index = Users.IndexOfKey("username1"); // check not found if necessary
var nextItemKey = s.Keys[(index + 1) % Users.Count()];
var nextItemValue = Users.IndexOfKey(nextItemKey);发布于 2015-05-14 22:55:13
SortedList包含执行此操作所需的方法。看看IndexOfKey和GetByIndex。
https://stackoverflow.com/questions/30248781
复制相似问题