我有以下方法用来自数据阅读器的值填充字典。数据读取器字段和传递给方法的属性之间可能存在大小写不匹配。在以下方法中,我首先将属性转换为小写以解决此问题。这会导致两个字典。有没有更好的方法可以用一本字典来实现这一目标?
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;
}