转换 IDictionary; 键转为小写 (C#)
我有一个获取 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
是 - 传递 Dictionary 构造函数
StringComparer.OrdinalIgnoreCase
(或另一个忽略大小写的比较器,具体取决于您的文化敏感性需求)。Yes - pass the Dictionary constructor
StringComparer.OrdinalIgnoreCase
(or another case-ignoring comparer, depending on your culture-sensitivity needs).通过使用 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
您可以使用 var 关键字来消除一些混乱。 从技术上讲,来源保持不变。 另外,我只会传递并返回一个 Dictionary因为您没有对 ILanguage 参数执行任何操作并使该方法更具可重用性:
...并像这样调用它:
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:
... and call it like so:
您可以自己继承 IDictionary,并简单地将调用编组到内部 Dictionary 实例。
You could inherit from IDictionary yourself, and simply marshal calls to an internal Dictionary instance.
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."
你也可以尝试这个方法
You can also try this way
使用
IEnumerable
扩展方法的 LINQ 版本:LINQ version using the
IEnumerable<T>
extension methods: