Iteration through SortedList

2019-09-04 08:52发布

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!

标签: c# sortedlist
7条回答
对你真心纯属浪费
2楼-- · 2019-09-04 08:59

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

查看更多
何必那么认真
3楼-- · 2019-09-04 08:59

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

查看更多
相关推荐>>
4楼-- · 2019-09-04 09:01

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楼-- · 2019-09-04 09:06

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);
    }
查看更多
Evening l夕情丶
6楼-- · 2019-09-04 09:13

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;
}
查看更多
贼婆χ
7楼-- · 2019-09-04 09:15

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);
查看更多
登录 后发表回答