更好的code,避免在同一个字典 - 大小写敏感性问题 [英] Better code for avoiding one dictionary - Case Sensitivity Issue

查看:238
本文介绍了更好的code,避免在同一个字典 - 大小写敏感性问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下方法来填充一个字典,从数据读取器的值。可以存在该数据读取器字段和属性传递给方法的情况下之间不匹配。在下面的方法,我将在第一个属性为小写来解决这个问题。这将导致两个字典。有没有更好的办法有一个字典来实现这一目标?

 专用字典<字符串,对象> FillDictionaryWithReaderValues​​(名单<字符串> propertiesOfAllEntities,IDataReader的读者)
{
   字典<字符串,对象> lowerCaseDictionary =新字典<字符串,对象>();
   字典<字符串,对象> propertyResultList =新字典<字符串,对象>();

   的foreach(在propertiesOfAllEntities VAR项)
   {
      lowerCaseDictionary.Add(item.ToLower(),NULL);
   }

   的for(int i = 0; I< reader.FieldCount;我++)
   {
      lowerCaseDictionary [reader.GetName(ⅰ).ToLower()] =读者[I];
   }

   的foreach(在propertiesOfAllEntities VAR项)
   {
      propertyResultList.Add(项目,lowerCaseDictionary [item.ToLower());
   }

   返回propertyResultList;
}
 

解决方案

感谢@CuongLe。请upvote @Cuong乐的答案,如果你喜欢下面的内容。

有关其他人的利益,我会在这里写答案:

 专用字典<字符串,对象> FillDictionaryWithReaderValues​​(名单<字符串> propertiesOfAllEntities,IDataReader的读者)
    {

        字典<字符串,对象> propertyResultList =新字典<字符串,对象>(StringComparer.InvariantCultureIgnoreCase);
        的for(int i = 0; I< reader.FieldCount;我++)
        {
            串readerFieldName = reader.GetName(ⅰ);
            //无论propertiesOfAllEntities.Contains物业
            如果(propertiesOfAllEntities.FindIndex(X =>!x.Equals(readerFieldName,StringComparison.OrdinalIgnoreCase))= -1)
            {
                propertyResultList.Add(readerFieldName,读者[I]);
            }

        }

        返回propertyResultList;
    }
 

参考文献:

  1. 不区分大小写的列表搜索

I have following method to fill a dictionary with values from a data reader. There can be case mismatches between the data-reader fields and properties passed to the method. In the following method I am converting the properties to lowercase first to address this issue. This causes two dictionaries. Is there a better way to achieve this with one dictionary?

private Dictionary<string, object> FillDictionaryWithReaderValues(List<string> propertiesOfAllEntities, IDataReader reader)
{
   Dictionary<string, object> lowerCaseDictionary = new Dictionary<string, object>();
   Dictionary<string, object> propertyResultList = new Dictionary<string, object>();

   foreach (var item in propertiesOfAllEntities)
   {
      lowerCaseDictionary.Add(item.ToLower(), null);
   }

   for (int i = 0; i < reader.FieldCount; i++)
   {
      lowerCaseDictionary[reader.GetName(i).ToLower()] = reader[i];
   }

   foreach (var item in propertiesOfAllEntities)
   {
      propertyResultList.Add(item, lowerCaseDictionary[item.ToLower()]);
   }

   return propertyResultList;
}

解决方案

Thanks to @CuongLe. Please upvote @Cuong Le answer if you like the following.

For the benefit of others I will write the answer here:

    private Dictionary<string, object> FillDictionaryWithReaderValues(List<string> propertiesOfAllEntities, IDataReader reader)
    {

        Dictionary<string, object> propertyResultList = new Dictionary<string, object>(StringComparer.InvariantCultureIgnoreCase);
        for (int i = 0; i < reader.FieldCount; i++)
        {
            string readerFieldName = reader.GetName(i);
            //Whether propertiesOfAllEntities.Contains the property
            if (propertiesOfAllEntities.FindIndex(x => x.Equals(readerFieldName, StringComparison.OrdinalIgnoreCase)) != -1)
            {
                propertyResultList.Add(readerFieldName, reader[i]);
            }

        }

        return propertyResultList;
    }

REFERENCE:

  1. Case-Insensitive List Search

这篇关于更好的code,避免在同一个字典 - 大小写敏感性问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆