Q:

Updating Particular Key/Values in a Hash Table - Redis C# .NET Core 2.2

I am new to Redis and I am trying to do the usual, which is convert a single SQL table to Redis.  More specifically, after doing some reading, I came across hset, which I translated into C# .Net Core with StackExchange.Redis.
I also came across Sorted Sets, which after reading stated they would be necessary for searching for specific things, but after finding some example code Here it doesn't seem to be what I am looking for (unless I'm incorrect)
Basically, I have an API that takes in 4 parameters, a device no, input no, state (0 or 1) and a datetime.  Once the device and input have been looked up, the state will updated in accordance with the parameter.  E.g. device 1, input 12, state 1, datetime "xxx".
My code so far is this; however, this only pertains to one row/hash entry, there will be a total of 12 inputs for each device and a total of 16 devices, so there will be a total of 192 rows/hash Entries.
            string state = "state:1";

            HashEntry[] states =
            {
                new HashEntry("id", 1),
                new HashEntry("device", 1),
                new HashEntry("input", 1),
                new HashEntry("state", 0),
                new HashEntry("when", "2019-10-23 14:22:48.5174055 +01:00")

            };

            db.HashSet(state, states);

            var allStates = db.HashGetAll(state);

            foreach(var item in allStates)
            {
                Console.WriteLine($"{item.Name}, {item.Value}");
            }

Such code will be in some form of loop in order to fulfil the 192 rows/hash entries, but I thought I would code this just to start.  So I thought I would seek some guidance, as the way in which I am approaching this could be incorrect, or, I am missing something such as a Sorted Set.

A:

Hashes are a good place to start, but I would suggest using different keys to segment your data rather than having to iterate through all entries. Set a new key for each input of a device (192 keys in total): 
device1:input1 
device1:input2
device1:input3
...

Each key would then store your hash entries. To update a field for a particular key, you would only need to execute a single hash-set command, for example:
db.HashSet("device6:input2", "when", DateTime.Now.ToString());

Or to update multiple fields:
db.HashSet("device6:input2", new HashEntry[] 
{ 
    new HashEntry("state", 1), 
    new HashEntry("when", DateTime.Now.ToString())
});

If you just wanted to retrieve a device's data, it's pretty easy as well:
db.HashGet("device6:input2", "your-field-name"); // gets a single field's value
db.HashGetAll("device6:input2"); // gets the values from all fields

This will give you the simplest implementation and will save you from having to iterate through many entries.

