Convert String[] array to RedisKey[] array

Justin Homes picture Justin Homes · Feb 9, 2015 · Viewed 7.5k times · Source

Trying to use

KeyDelete(RedisKey[] keys, CommandFlags flags = CommandFlags.None);

I have array of string[] , I am not seeing any examples out there when I search for converting these data types. I am not even sure how to create a new RedisKey.

Tried

RedisKey redisKey = new RedisKey("d");

above does not work, any suggestions?

Answer

dav_i picture dav_i · Feb 9, 2015

From the source code RedisKey has an implicit conversion from string:

/// <summary>
/// Create a key from a String
/// </summary>
public static implicit operator RedisKey(string key)
{
    if (key == null) return default(RedisKey);
    return new RedisKey(null, key);
}

So you can create one by

RedisKey key = "hello";

or

var key = (RedisKey)"hello";

To convert an IEnumerable<string> to RedisKey[], you can do:

var keys = strings.Select(key => (RedisKey)key).ToArray();