2

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;
}
LCJ
  • 22,196
  • 67
  • 260
  • 418
  • 2
    I have the feeling that it can be refactored earlier. What are you actually doing here? The whole method might be redundant. – Tim Schmelter Nov 06 '12 at 08:21
  • Why do you need 2 dictionaries? If you just stick to the convention that the key is always lowercase, you don't really need the other dictionary, right? – Roy Dictus Nov 06 '12 at 08:21
  • The case-insensitive-ness of the dictionary isn't really the issue here; it would let you remove the calls to `ToLower`, but won't stop you needing two dictionaries? (You'll still need to perform the don't-include-any-properties-that-were-in-the-reader-but-not-the-`propertyResultList` step, unless this isn't important.) – Rawling Nov 06 '12 at 08:27
  • @Rawling Can't we use `List propertiesOfAllEntities` for that purpose? – LCJ Nov 06 '12 at 08:37

2 Answers2

7

You can ignore case in Dictionary, Dictionary has an overload constructor which accepts IEqualityComparer, use StringComparer.InvariantCultureIgnoreCase to ignore case-sensitive for key:

var dic = 
      new Dictionary<string, object>(StringComparer.InvariantCultureIgnoreCase);

So only one dictionary with ignore case is enough for your code

cuongle
  • 74,024
  • 28
  • 151
  • 206
1

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
Community
  • 1
  • 1
LCJ
  • 22,196
  • 67
  • 260
  • 418