转换 IDictionary; 键转为小写 (C#)

发布于 2024-07-07 07:01:01 字数 1082 浏览 12 评论 0原文

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

所以我现在对此的解决方案是有一个静态函数,它循环遍历键并将它们转换为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;
}

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

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.

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

古镇旧梦 2024-07-14 07:01:01

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

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

风尘浪孓 2024-07-14 07:01:01

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

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

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

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

萌能量女王 2024-07-14 07:01:01

您可以使用 var 关键字来消除一些混乱。 从技术上讲,来源保持不变。 另外,我只会传递并返回一个 Dictionary因为您没有对 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;
}

...并像这样调用它:

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

You could use the var keyword 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:

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);
}
夜唯美灬不弃 2024-07-14 07:01:01

您可以自己继承 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.

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

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.
樱花细雨 2024-07-14 07:01:01

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

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

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

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

"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."

ゞ花落谁相伴 2024-07-14 07:01:01

你也可以尝试这个方法

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

You can also try this way

convertedDictionatry = convertedDictionatry .ToDictionary(k => k.Key.ToLower(), k => k.Value.ToLower());
爱要勇敢去追 2024-07-14 07:01:01

使用 IEnumerable 扩展方法的 LINQ 版本:


        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);
        }

LINQ version using the IEnumerable<T> extension methods:


        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);
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文