Justin Homes
Justin Homes

Reputation: 3799

Convert Dictionary to HashEntry

I am trying to use hashSet method and it needs HashEntry[] array.

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

I am trying to do this, but this obviously is not working...

I have Dictionary value

  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++;
  }

Upvotes: 4

Views: 4400

Answers (2)

Marc Gravell
Marc Gravell

Reputation: 1064184

A HashEntry is immutable; you need:

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

Or perhaps more convenienty:

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

Out of curiosity, what is the exact type of Dictionary<TKey,TValue> here? It could be reasonable to add a few overloads for convenience. There are already some convenience methods in the other direction, such as ToDictionary(...) and ToStringDictionary(...).

Upvotes: 7

Swapan Mohan
Swapan Mohan

Reputation: 9

var result = new HashSet(dictionarySet.Values);

Also refer below link:

C#: Dictionary values to hashset conversion

Upvotes: 0

Related Questions