Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 修剪字典键值扩展时出现异常_C#_Dictionary - Fatal编程技术网

C# 修剪字典键值扩展时出现异常

C# 修剪字典键值扩展时出现异常,c#,dictionary,C#,Dictionary,我有一个包含键值的字典,我正在尝试修剪这些值的扩展名,但得到一条异常消息,即已添加具有相同键值的项 不知道为什么会这样 这是我使用的代码 我怎样才能克服这个问题 dictFilesNotThere = dictFilesNotThere.ToDictionary (t => t.Key.Remove(8, 3), t => t.Value); 键值如下所示“7dim-058-ns”,我试图将其改为“7dim-058”如果同一个键值已经存在,则必须指定要保留的

我有一个包含键值的字典,我正在尝试修剪这些值的扩展名,但得到一条异常消息,即已添加具有相同键值的项

不知道为什么会这样

这是我使用的代码

我怎样才能克服这个问题

dictFilesNotThere = dictFilesNotThere.ToDictionary
            (t => t.Key.Remove(8, 3), t => t.Value);

键值如下所示“7dim-058-ns”,我试图将其改为“7dim-058”

如果同一个键值已经存在,则必须指定要保留的键值,例如第一个:

dictFilesNotThere.Select(kv => new { kv, newkey = kv.Key.Substring(0, 8) })
            .GroupBy(x => x.newkey)
            .ToDictionary(g => g.Key, g => g.First().kv.Value);

如果已存在相同的密钥,则必须指定要保留的密钥,例如第一个:

dictFilesNotThere.Select(kv => new { kv, newkey = kv.Key.Substring(0, 8) })
            .GroupBy(x => x.newkey)
            .ToDictionary(g => g.Key, g => g.First().kv.Value);

假设字典中有以下项目:

dictFilesNotThere.Add("7dim-058-ns", 1);
dictFilesNotThere.Add("7dim-058-n2", 2);
dictFilesNotThere.Add("7dim-058-n3", 2);
然后通过
t.Key.移除(8,3)
移除后,您将获得:
7dim-058
作为上述所有项目的密钥。因为在字典中不能有重复的键,这就是例外的原因

要解决此问题,您可以设置一个
计数器
,并将其添加到密钥中,前提是以前在字典中找到了密钥。比如:

Dictionary<string, int> dictFilesNotThereCopy = new Dictionary<string, int>();
int counter = 0;
foreach (KeyValuePair<string,int> item in dictFilesNotThere)
{
    if (dictFilesNotThereCopy.ContainsKey(item.Key.Remove(8, 3)))
        dictFilesNotThereCopy.Add((item.Key.Remove(8, 3) + (counter++)).ToString(), item.Value);
    else
        dictFilesNotThereCopy.Add(item.Key.Remove(8, 3), item.Value);
}
Dictionary dictfiles notthereport=new Dictionary();
int计数器=0;
foreach(dictFilesNotThere中的KeyValuePair项)
{
if(dictfilesnottherecompy.ContainsKey(item.Key.Remove(8,3)))
dictfilesNotTheRecy.Add((item.Key.Remove(8,3)+(counter++).ToString(),item.Value);
其他的
dictfilesNotTheRecy.Add(item.Key.Remove(8,3),item.Value);
}

假设词典中有以下项目:

dictFilesNotThere.Add("7dim-058-ns", 1);
dictFilesNotThere.Add("7dim-058-n2", 2);
dictFilesNotThere.Add("7dim-058-n3", 2);
然后通过
t.Key.移除(8,3)
移除后,您将获得:
7dim-058
作为上述所有项目的密钥。因为在字典中不能有重复的键,这就是例外的原因

要解决此问题,您可以设置一个
计数器
,并将其添加到密钥中,前提是以前在字典中找到了密钥。比如:

Dictionary<string, int> dictFilesNotThereCopy = new Dictionary<string, int>();
int counter = 0;
foreach (KeyValuePair<string,int> item in dictFilesNotThere)
{
    if (dictFilesNotThereCopy.ContainsKey(item.Key.Remove(8, 3)))
        dictFilesNotThereCopy.Add((item.Key.Remove(8, 3) + (counter++)).ToString(), item.Value);
    else
        dictFilesNotThereCopy.Add(item.Key.Remove(8, 3), item.Value);
}
Dictionary dictfiles notthereport=new Dictionary();
int计数器=0;
foreach(dictFilesNotThere中的KeyValuePair项)
{
if(dictfilesnottherecompy.ContainsKey(item.Key.Remove(8,3)))
dictfilesNotTheRecy.Add((item.Key.Remove(8,3)+(counter++).ToString(),item.Value);
其他的
dictfilesNotTheRecy.Add(item.Key.Remove(8,3),item.Value);
}

您需要为潜在的密钥冲突制定新的计划;你的“希望没有”计划失败了。另外,你真的在查字典吗?如果不是,你可以考虑一个<代码>列表>代码>,它会很高兴地允许重复的“密钥”。<代码> Debug文件。GroupBy(t=>T.KEY。删除(8, 3))。在哪里(G=>G CONTUTE())1。SelectMany(t=>T.KEY)< /Cord>这个查询将显示问题密钥。如果你绝对需要DUPE键,考虑看。TooOkUp,而不是toTo字典。本质上,是一种字典类型的结构,允许每个键有多个项。最后一个问题:修剪的目的是什么,为什么需要在字典中修剪结果?您已经向我们展示了当前解决方案的一个问题,但我认为如果您向我们展示了您试图解决的全部问题,我们会有更好的帮助;你的“希望没有”计划失败了。另外,你真的在查字典吗?如果不是,你可以考虑一个<代码>列表>代码>,它会很高兴地允许重复的“密钥”。<代码> Debug文件。GroupBy(t=>T.KEY。删除(8, 3))。在哪里(G=>G CONTUTE())1。SelectMany(t=>T.KEY)< /Cord>这个查询将显示问题密钥。如果你绝对需要DUPE键,考虑看。TooOkUp,而不是toTo字典。本质上,是一种字典类型的结构,允许每个键有多个项。最后一个问题:修剪的目的是什么,为什么需要在字典中修剪结果?您已经向我们展示了您当前解决方案的一个问题,但我认为如果您向我们展示您试图解决的全部问题,我们会有更好的帮助。