c#排序列表按键组获取元素

c#排序列表按键组获取元素,c#,sortedlist,C#,Sortedlist,我有一个实现排序列表的类: public class Plates : IEnumerable<KeyValuePair<string, Plate>> { private SortedList<string, Plate> sortedPlates; public Plates() { sortedPlates = new SortedList<string, Plate>(new KeyComparer())

我有一个实现排序列表的类:

public class Plates : IEnumerable<KeyValuePair<string, Plate>> {

    private SortedList<string, Plate> sortedPlates;

    public Plates() {
        sortedPlates = new SortedList<string, Plate>(new KeyComparer());
       
    }

   ...some additiona data such getters and setters

}
我的问题是如何通过索引高效地获取对象组。例如,我想检索所有具有第一个索引为0的键和另一个索引以2开头的组的对象。 第一个想法是,我可以创建一个散列集,然后在将对象插入字典时对对象进行分组,但我真的认为这样做效率不高:

    HashSet<int> hash = new HashSet<int>();
    Dictionary<int, List<string>> groups = new Dictionary<int, List<string>>();

    public void Add(Element item) {

        string key = item.GetKey();
        sortedPlates.Add(key, item);


        int firstID = item.KeyToIntArray()[0];
        if (hash.Add(firstID)) 
            groups.Add(firstID,new List<string> { key });
        else 
            groups[firstID].Add(key );

    }
HashSet hash=newhashset();
字典组=新字典();
公共无效添加(元素项){
string key=item.GetKey();
分类板。添加(键、项);
int firstID=item.KeyToIntArray()[0];
if(hash.Add(firstID))
Add(firstID,新列表{key});
其他的
组[firstID]。添加(键);
}
有没有办法不通过循环直接访问组? 我是否需要向曲目组添加其他属性?
我觉得这种双循环对于大量的对象是无效的。

LINQ是一种选择吗?它不是关于更快的重写,而是更有效的分组。我在问题描述的最后部分添加了一段代码,包括哈希和字典。除了检查是否已经放置了这样的组索引“firstID”之外,还有其他方法吗?而不是向hashset添加元素以避免重复。如果已经对其进行了排序,您能否只存储组0开始和结束的索引,以及组2开始和结束的索引?亲爱的@Daniel,我该怎么做?我可能遗漏了一些简单的逻辑选项?这不是关于更快的重写,而是更有效的分组。我在问题描述的最后部分添加了一个代码,包括哈希和字典。除了检查是否已经放置了这样的组索引“firstID”之外,还有其他方法吗?而不是向hashset添加元素以避免重复。如果已经对其进行了排序,您能否只存储组0开始和结束的索引,以及组2开始和结束的索引?亲爱的@Daniel,我该怎么做?我可能缺少一些简单的逻辑
[0;0]
[0;0;0]
[0;1]
[0;1;1;0;0]
[0;2]
[0;8;1;0;0]
[0;9;1;0;0]
[0;10]
[0;10;1;0;0]
[1;0]
[1;1]
[2;1]
[2;10]
[3;1]
[3;1;2]
[3;1;4]
    HashSet<int> hash = new HashSet<int>();
    Dictionary<int, List<string>> groups = new Dictionary<int, List<string>>();

    public void Add(Element item) {

        string key = item.GetKey();
        sortedPlates.Add(key, item);


        int firstID = item.KeyToIntArray()[0];
        if (hash.Add(firstID)) 
            groups.Add(firstID,new List<string> { key });
        else 
            groups[firstID].Add(key );

    }