Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 正在加载特定类型的字典值_C#_.net_C# 4.0 - Fatal编程技术网

C# 正在加载特定类型的字典值

C# 正在加载特定类型的字典值,c#,.net,c#-4.0,C#,.net,C# 4.0,如何在读取不同的datatable调用时加载具有特定类型成员的字典 我正在使用HashSet.ToDictionary方法填充字典 然后我调用DB并希望加载对象属性值1 最后,我将调用另一个DB,查找并预填充现有对象的Value2属性 HashSetHashSet=新的HashSet; DictionaryDictionary=新字典; dictionary=hashset.ToDictionaryh=>h,h=>CustomeObject null; 当第一个读者读的时候{ 如果diction

如何在读取不同的datatable调用时加载具有特定类型成员的字典

我正在使用HashSet.ToDictionary方法填充字典 然后我调用DB并希望加载对象属性值1 最后,我将调用另一个DB,查找并预填充现有对象的Value2属性 HashSetHashSet=新的HashSet; DictionaryDictionary=新字典; dictionary=hashset.ToDictionaryh=>h,h=>CustomeObject null; 当第一个读者读的时候{ 如果dictionary.ContainsKeyfirstreader.GetValue1.ToString{ 字典[firstreader.GetValue1.ToString]=新的CustomeObject{ Key=firstreader.GetValue1.ToString, Value1=firstreader.GetValue2.ToString, Value2=null }; } } 边读边读{ 如果dictionary.ContainsKeysecondreader.GetValue1.ToString{ 字典[secondreader.GetValue1.ToString]=新的CustomeObject{ Key=,//保留上一次加载的值 Value1=secondreader.GetValue2.ToString, Value2=null; }; } }
当您在迭代两个读取器的同时写入字典时,您正在两个读取器中实例化一个新对象并将其分配给字典值。因此,您在读取firstreader时创建对象,然后在读取secondreader时覆盖对象

阅读secondreader时需要做的是检查字典值是否为null。如果不是,那么只写Value2

while (secondreader.Read()) {
  var myReadValue = secondreader.GetValue(1).ToString();
  if (dictionary.ContainsKey(myReadValue)) {
    if(dictionary[myReadValue] != null) {
      dictionary[myReadValue].Value2 = secondreader.GetValue(2).ToString();
    }
  }
}