Iteration through SortedList

雨燕双飞 提交于 2019-12-11 20:32:14

问题


SortedList<string, systemuser> Users = new SortedList<string, systemuser>();
Users.Add("username1",customobject);
Users.Add("username2",customobject);
Users.Add("username3",customobject);

What i'm trying to do is search the list for a specific key (i.e. a username) and return the next item in the list. If i'm at the end of the list then I need to return the item in the first position.

Not sure how to go about this, as the SortedList doesn't seem to expose an index property.

I have:

foreach (KeyValuePair<string, systemuser> pair in Users)
{
    if (pair.Key == lastAllocation)
    {
        // RETURN THE ITEM IMMEDIATELY AFTER THIS ONE
    }
}

Any suggestion appreciated!


回答1:


Are you looking for the next item (KeyValuePair), value, or index?

bool returnNextItem = false;
foreach (KeyValuePair<string, systemuser> pair in Users)
{
    if (returnNextItem)
        return pair.Value;
    if (pair.Key == lastAllocation)
        returnNextItem = true;
}



回答2:


First get the index of the seeking element. Then get the key for the necessary one (don't forget the module %). Finally access the loved one :))

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



回答3:


The SortedList contains the methods necessary to do this. Look at IndexOfKey and GetByIndex.




回答4:


You can use IndexOfKey to find the position in the list of the given username.

Using the index returned, you can return the next item, or return the first item in the list.

var idx = Users.IndexOfKey("username2");

if (idx == (Users.Count - 1)){
    return Users.First().Key;
}
else{
    return Users.Keys[idx + 1];
    //.net 4.5+ can Users.GetByIndex()
}



回答5:


You could try this. Not sure what you want if there is no match so we are just returning an new KeyValuePair; Also make sure you have a using System.Linq; so you have access the ElementAt extension method.

   public static KeyValuePair<string, string> GetUserInfo(string username, SortedList<string, string> list)
    {
        int index = list.IndexOfKey(username);
        if(index == -1)
        {
            return new KeyValuePair<string, string>();
        }

        index = index == (list.Count - 1) ? 0 : index + 1;

        return list.ElementAt(index);
    }



回答6:


When you call foreach, what you're really doing is calling Users.GetEnumerator();.

That is syntactic sugar for the following:

KeyValuePair<String, Systemuser> pair;
IEnumerator<KeyValuePair<String, Systemuser>> enumerator = Users.GetEnumerator();
while (enumerator.MoveNext())
{
    pair = enumerator.Current;
    // your code in the foreach loop here
}

Easy solution is to use the enumerator manually to do what you want.

KeyValuePair<String, Systemuser> getSomething(String lastAllocation)
{

    IEnumerator<KeyValuePair<String, Systemuser>> enumerator = Users.GetEnumerator();
    while (enumerator.MoveNext())
    {
        if (enumerator.Current.Key == lastAllocation)
        {
            enumerator.MoveNext();
            return enumerator.Current; // null if not another
        }
    }
    return null;
}



回答7:


For reference, there are 2 core references for SortedList, one in System.Collections (older) and System.Collections.Generic. When dealing with System.Collections.SortedList, I was only able to use the method of iteration referenced in the Microsoft Article (https://msdn.microsoft.com/en-us/library/system.collections.sortedlist(v=vs.110).aspx)

Here is the loop where myList is the SortedList collection.

  for ( int i = 0; i < myList.Count; i++ )  {

     Console.WriteLine( "\t{0}:\t{1}", myList.GetKey(i), myList.GetByIndex(i) );

  }

Dave



来源:https://stackoverflow.com/questions/30248781/iteration-through-sortedlist

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!