将 IDictionary<string, string> 键转换为小写 (C#)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/161556/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 16:15:10  来源:igfitidea点击:

Convert IDictionary<string, string> keys to lowercase (C#)

c#.netidictionary

提问by Tigraine

I've got a Method that gets a IDictionary as a parameter. Now I want to provide a method that retrieves the value from this dictionary, but it should be case-invariant.

我有一个获取 IDictionary 作为参数的方法。现在我想提供一个方法来从这个字典中检索值,但它应该是大小写不变的。

So my solution to this right now was to have a static function that loops through the keys and converts them toLower() like this:

所以我现在对此的解决方案是有一个静态函数,它循环遍历键并将它们转换为Lower(),如下所示:

private static IDictionary<ILanguage, IDictionary<string, string>> ConvertKeysToLowerCase(
    IDictionary<ILanguage, IDictionary<string, string>> dictionaries)
{
    IDictionary<ILanguage, IDictionary<string, string>> resultingConvertedDictionaries 
        = new Dictionary<ILanguage, IDictionary<string, string>>();
    foreach(ILanguage keyLanguage in dictionaries.Keys)
    {
        IDictionary<string, string> convertedDictionatry = new Dictionary<string, string>();
        foreach(string key in dictionaries[keyLanguage].Keys)
        {
            convertedDictionatry.Add(key.ToLower(), dictionaries[keyLanguage][key]);
        }
        resultingConvertedDictionaries.Add(keyLanguage, convertedDictionatry);
    }
    return resultingConvertedDictionaries;
}

Now, this is ok, but still it's a pretty huge chunk of code that contradicts my idea of "clean and efficient". Do you know any alternatives to this so that the .ContainsKey() method of the dictionary doesn't differentiate between casing?

现在,这没问题,但它仍然是一大块代码,与我的“干净和高效”的想法相矛盾。你知道这个的任何替代方法,以便字典的 .ContainsKey() 方法不区分大小写吗?

采纳答案by Jon Skeet

Yes - pass the Dictionary constructor StringComparer.OrdinalIgnoreCase(or another case-ignoring comparer, depending on your culture-sensitivity needs).

是 - 传递 Dictionary 构造函数StringComparer.OrdinalIgnoreCase(或另一个忽略大小写的比较器,具体取决于您的文化敏感性需求)。

回答by VVS

You could use the varkeyword to remove some clutter. Technically the source remains the same. Also I would just pass and return a Dictionary<string, string> because you're not doing anything with that ILanguage parameter and make the method more reusable:

您可以使用var关键字来消除一些混乱。从技术上讲,来源保持不变。此外,我只会传递并返回一个 Dictionary<string, string> ,因为您没有对该 ILanguage 参数执行任何操作并使该方法更可重用:

private static IDictionary<string, string> ConvertKeysToLowerCase(
    IDictionary<string, string> dictionaries)
{
    var convertedDictionatry = new Dictionary<string, string>();
    foreach(string key in dictionaries.Keys)
    {
        convertedDictionatry.Add(key.ToLower(), dictionaries[key]);
    }
    return convertedDictionatry;
}

... and call it like so:

...并像这样称呼它:

// myLanguageDictionaries is of type IDictionary<ILanguage, IDictionary<string, string>>
foreach (var dictionary in myLanguageDictionaries.Keys)
{
  myLanguageDictionaries[dictionary].Value = 
      ConvertKeysToLowerCase(myLanguageDictionaries[dictionary].Value);
}

回答by Jonathan C Dickinson

You could inherit from IDictionary yourself, and simply marshal calls to an internal Dictionary instance.

您可以自己从 IDictionary 继承,并简单地编组对内部 Dictionary 实例的调用。

Add(string key, string value) { dictionary.Add(key.ToLowerInvariant(), value) ; }
public string this[string key]
{
    get { return dictionary[key.ToLowerInvariant()]; }
    set { dictionary[key.ToLowerInvariant()] = value; }
}
// And so forth.

回答by mancaus

LINQ version using the IEnumerable<T>extension methods:

LINQ 版本使用IEnumerable<T>扩展方法:


        private static IDictionary<ILanguage, IDictionary<string, string>> ConvertKeysToLowerCase(
            IDictionary<ILanguage, IDictionary<string, string>> dictionaries)
        {
            return dictionaries.ToDictionary(
                x => x.Key, v => CloneWithComparer(v.Value, StringComparer.OrdinalIgnoreCase));
        }

        static IDictionary<K, V> CloneWithComparer<K,V>(IDictionary<K, V> original, IEqualityComparer<K> comparer)
        {
            return original.ToDictionary(x => x.Key, x => x.Value, comparer);
        }

回答by GregUzelac

System.Collections.Specialized.StringDictionary() may help. MSDN states:

System.Collections.Specialized.StringDictionary() 可能会有所帮助。MSDN 指出:

"The key is handled in a case-insensitive manner; it is translated to lowercase before it is used with the string dictionary.

“密钥以不区分大小写的方式处理;在与字符串字典一起使用之前,它会被转换为小写。

In .NET Framework version 1.0, this class uses culture-sensitive string comparisons. However, in .NET Framework version 1.1 and later, this class uses CultureInfo.InvariantCulture when comparing strings. For more information about how culture affects comparisons and sorting, see Comparing and Sorting Data for a Specific Culture and Performing Culture-Insensitive String Operations."

在 .NET Framework 1.0 版中,此类使用区分区域性的字符串比较。但是,在 .NET Framework 1.1 及更高版本中,此类在比较字符串时使用 CultureInfo.InvariantCulture。有关区域性如何影响比较和排序的详细信息,请参阅比较和排序特定区域性的数据和执行与区域性无关的字符串操作。”

回答by GregUzelac

By using a StringDictionary the keys are converted to lower case at creating time.

通过使用 StringDictionary,键在创建时被转换为小写。

http://simiansoftware.blogspot.com/2008/11/have-const-string-with-ui-description.html

http://simiansoftware.blogspot.com/2008/11/have-const-string-with-ui-description.html

回答by ANJYR - KODEXPRESSION

You can also try this way

你也可以试试这个方法

convertedDictionatry = convertedDictionatry .ToDictionary(k => k.Key.ToLower(), k => k.Value.ToLower());