Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass an array to StackExchange.Redis method taking RedisValue[]?

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?

like image 830
Gigi Avatar asked Feb 24 '15 16:02

Gigi


1 Answers

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" };
like image 146
guillem Avatar answered Nov 15 '22 16:11

guillem