6

I'd like to call IDatabase.SortedSetAsync (StackExchange.Redis). The signature for this method is:

Task<bool> SortedSetRemoveAsync(RedisKey key, RedisValue member,
        CommandFlags flags = CommandFlags.None);
Task<long> SortedSetRemoveAsync(RedisKey key, RedisValue[] members,
        CommandFlags flags = CommandFlags.None);

I have no problem passing a single value (e.g. a string) which gets implicitly converted to RevisValue. However, if I try passing in an array...

var key = "MYKEY";
var values = new string[] { "A", "B", "C" };

await database.SortedSetRemoveAsync(key, values);

...then the compiler has no idea how to convert the string[] to a RedisValue[].

The only way I managed the conversion is by making a per-item conversion:

var values2 = values.Select(s => (Redisvalue) s).ToArray();
await database.SortedSetRemoveAsync(key, values2);

...and I'm not sure whether that's the best way to do it.

It's quite strange that it's so hard to pass an array when there are API methods expecting arrays... the unit tests take an easy direction by passing in an empty RedisValue[] array and bypassing the problem altogether.

What is the recommended way to pass array arguments to StackExchange.Redis methods accepting RedisValue[] arrays?

1 Answer 1

15

The select solution is correct but it would be more efficient using Array.ConvertAll as it knows array elements and can iterate elements by index.

var values = new string[] { "A", "B", "C" };
var redisValues = Array.ConvertAll(values, item => (RedisValue)item);

Notice you can also create the list like

RedisValue[] values = { "A", "B", "C" };
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.