C# 字典。包含在特定的起始索引处

C# 字典。包含在特定的起始索引处,c#,dictionary,key,contains,C#,Dictionary,Key,Contains,如何检查字典是否包含从预定义键开始的值?例如,我只想在特定键之后搜索字典,而不是从第一个键开始搜索 我怎样才能做到这一点呢?不确定在这种情况下使用词典是否是最好的选择,因为(如前所述)不能保证顺序。但是,如果使用列表,则可以获得所需的行为。像这个例子: var items = new List<KeyValuePair<string, string>>(); items.Add(new KeyValuePair<string, str

如何检查字典是否包含从预定义键开始的值?例如,我只想在特定键之后搜索字典,而不是从第一个键开始搜索


我怎样才能做到这一点呢?

不确定在这种情况下使用词典是否是最好的选择,因为(如前所述)不能保证顺序。但是,如果使用
列表
,则可以获得所需的行为。像这个例子:

        var items = new List<KeyValuePair<string, string>>();
        items.Add(new KeyValuePair<string, string>("A", "1"));
        items.Add(new KeyValuePair<string, string>("B", "2"));
        items.Add(new KeyValuePair<string, string>("C", "3"));
        var results = items.Skip(1).Take(1).ToList();

        MessageBox.Show(results[0].Key + " " + results[0].Value);
var items=newlist();
添加(新的KeyValuePair(“A”、“1”));
添加(新的KeyValuePair(“B”、“2”));
添加(新的KeyValuePair(“C”、“3”));
var results=items.Skip(1).Take(1).ToList();
MessageBox.Show(结果[0]。键+“”+结果[0]。值);
在此示例中,使用
Skip
Take
方法显示的结果消息将是“b2”。它“跳过”第一个“1”并“接受”下一个“1”

编辑(重构为使用KeyValuePair列表而不是字符串)。

也许您需要的是一个,它提供了对插入顺序的控制。下面是一个示例,其中还包含一些搜索速度统计信息

using System;
using System.Collections;
using System.Collections.Specialized;
using System.Diagnostics;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            var startIndex = 1000000; // 00:00:00.0004876
            //var startIndex = 1;     // 00:00:00.0152319

            var searchedKey = "1";

            OrderedDictionary orderedDictionary = new OrderedDictionary();

            //populate
            for (int i = 2; i < 1000002; i++)
            {
                orderedDictionary.Add(i.ToString(), "X");
            }
            orderedDictionary.Add("1", "A");

            //copy the keys
            String[] keys = new String[1000006];
            orderedDictionary.Keys.CopyTo(keys, 0);

            //measure the time with a System.Diagnostics.StopWatch
            Stopwatch watch = new Stopwatch();
            watch.Start();

            for (int i = startIndex; i < orderedDictionary.Count; i++)
            {
                if (keys[i] == searchedKey)
                {
                    Console.WriteLine(orderedDictionary[i]);
                    break;
                }
            }
            watch.Stop();

            Console.WriteLine(watch.Elapsed);
        }
    }
}
使用系统;
使用系统集合;
使用System.Collections.Specialized;
使用系统诊断;
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
var startIndex=1000000;//00:00:00.0004876
//var startIndex=1;//00:00:00.0152319
var searchedKey=“1”;
OrderedDictionary OrderedDictionary=新的OrderedDictionary();
//填充
对于(int i=2;i<1000002;i++)
{
添加(i.ToString(),“X”);
}
orderedDictionary.添加(“1”、“A”);
//复制钥匙
字符串[]键=新字符串[1000006];
orderedDictionary.Keys.CopyTo(键,0);
//使用System.Diagnostics.StopWatch测量时间
秒表=新秒表();
watch.Start();
for(int i=startIndex;i
如果它是一本普通的
字典,那么你不能,除非你先对它排序


您可以使用其中一个或两个,按键排序。

您是否假设键是按添加顺序存储的?您如何定义“在一个键之后”?除了按键查找之外,还可以使用字典,这听起来像是滥用字典,需要另一种类型的集合。但正如@itsme86所说:请详细解释一下你的确切意思。关于
Dictionary
:“返回项的顺序未定义。”虽然这是指
IEnumerable
实现,但该语句通常对Dictionary中元素的顺序有效。它是未定义的。因此,诸如first或after之类的术语对于字典中的关键字来说毫无意义。此外,将不清楚单词的字体大小更改为粗体也不会使它们更清晰。我已编辑了您的标题。请参见“”,其中共识是“不,他们不应该”。我不认为这对OP有什么用处,因为我怀疑如果他不需要固有的键/值选择,他是否会使用字典。“你可能会建议他用你的方法来尝试列表。”JesseCarter:这是一个很好的建议。我只是想简单地展示一下Skip-and-Take功能,但是你的建议可能更有用。非常感谢。