2015-02-09 133 views
3

我正在尝试使用hashSet方法,它需要HashEntry []数组。将字典转换为HashEntry

HashSet(RedisKey key, HashEntry[] hashFields, CommandFlags flags = CommandFlags.None); 

我试图做到这一点,但是这显然是不工作...

我有字典值

HashEntry[] hash = new HashEntry[value.Count](); 
    int index = 0; 
    foreach (var item in value) 
    { 
     hash[index].Name = item.Key; 
     hash[index].Value = item.Value; 
     index++; 
    } 
+0

错误信息? – DrKoch 2015-02-09 06:45:43

回答

3

HashEntry是不可变的;您需要:

hash[index++] = new HashEntry(item.Key, item.Value); 

或者更convenienty:

var fields = dictionary.Select(
    pair => new HashEntry(pair.Key, pair.Value)).ToArray(); 

出于好奇,什么是确切类型的Dictionary<TKey,TValue>这里?为了方便起见,添加一些重载可能是合理的。另一方面已有一些便利方法,如ToDictionary(...)ToStringDictionary(...)

+0

它是Dictionary 2015-02-09 15:38:09