C# 字典中已添加具有相同键的项

C# 字典中已添加具有相同键的项,c#,.net,C#,.net,我正在开发sql clr应用程序,在与sql集成后,我发现英文版出现此错误。它可以工作,但在将Unicode添加到Dictionary时出现问题: **Msg 6522, Level 16, State 2, Line 1 A .NET Framework error occurred during execution of user-defined routine or aggregate "SqlCompare": System.ArgumentException: An item

我正在开发sql clr应用程序,在与sql集成后,我发现英文版出现此错误。它可以工作,但在将Unicode添加到Dictionary时出现问题:

**Msg 6522, Level 16, State 2, Line 1
A .NET Framework error occurred during execution of user-defined routine or     aggregate "SqlCompare": 
System.ArgumentException: An item with the same key has already been added.
System.ArgumentException: 
   at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
   at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value,      Boolean add)
   at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)
   at Translate_Class_NUM..ctor()
   at StringPercentageCompare..ctor()
   at UserDefinedFunctions.SqlComparison()**

 here is my code in c#
private   Dictionary<string, string> MyDictionary;
//private string[,] MyArray;

public Translate_Class_NUM()
{
    MyDictionary.Add("?", "01");
    MyDictionary.Add("?", "02");
    MyDictionary.Add("?", "03");
    MyDictionary.Add("?", "04");
    MyDictionary.Add("?", "05")
}
在sql server中,代码是 创建程序集DBDB_重复数据消除 授权dbo 来自“E:\Projects\DBDB\u DeDuplication\DBDB\u DeDuplication\obj\Debug\DBDB\u DeDuplication.dll” 具有权限\u SET=SAFE 去

创建函数SqlCompare返回nvarchar50 作为外部名称DBDB_DeDuplication.UserDefinedFunctions.SqlComparison; 去

选择dbo.SqlCompare; 去


提前感谢

此代码对我来说很好:

Dictionary<string, string> dict = new Dictionary<string, string>();
dict.Add("ಅ","01");
dict.Add("ಇ","02");
因此,您可以编写更安全的源代码版本,如下所示:

Dictionary<string, string> dict = new Dictionary<string, string>();
dict.Add("\u0c85","01");
dict.Add("\u0c87","02");

不能向同一个键添加多个值。MyDictionary.Add?,01;将值字符串01放在键下?下面的行将失败,因为它使用相同的键?。您可以改用MyDictionary[?]=01语法,它不会出错,但会覆盖该键下的先前值。您的字典使用有问题您是否切换了值和键?S了解?从某种意义上讲,unicode中的字符不同。。这里显示为?只有但在代码中,我使用的是不同的unicode字符。它适用于dictionary.adda,01 dictionary.addb,02,但不适用于unicode字符,如。。。dictionary.addಅ,01.addಇ,02尝试使用Unicode字符文字编码U+0000到U+FFFF。错误很明显,您的密钥是相同的。您是否使用ASCII编码保存源文件?如果是这样的话,您所有的Unicode字符都被向下转换为实际的问号。我对这项工作感到失望。。任何人都可以帮忙
Dictionary<string, string> dict = new Dictionary<string, string>();
dict.Add("\u0c85","01");
dict.Add("\u0c87","02");