C
C#9mo ago
Alex

✅ Redis.OM create key from 2 fields

I have a class
public class Sensor:Entity
{
public int Id { get; set; }
public int StationId { get; set; }
public string Name { get; set; }
[Indexed(Sortable = true)]
public DateTime? TimeWindowStart { get; set; }
[Indexed(Sortable = true)]
public DateTime? TimeWindowEnd { get; set; }
public DateTime? MeasuredTime { get; set; }
public string Unit { get; set; }
public int Value { get; set; }
}
public class Sensor:Entity
{
public int Id { get; set; }
public int StationId { get; set; }
public string Name { get; set; }
[Indexed(Sortable = true)]
public DateTime? TimeWindowStart { get; set; }
[Indexed(Sortable = true)]
public DateTime? TimeWindowEnd { get; set; }
public DateTime? MeasuredTime { get; set; }
public string Unit { get; set; }
public int Value { get; set; }
}
I want to create key that contains two fields stationId and Id, for example $"{stationId}{id}". How can I do it?
8 Replies
Pobiega
Pobiega9mo ago
Make a calculated get only property?
Alex
AlexOP9mo ago
Okay, I'll try it, thank you
Pobiega
Pobiega9mo ago
public string CombinedIds => $"{Id}_{StationID}";
Alex
AlexOP9mo ago
I set an id on Sensor object creation because Senser is inherited from class Entity which has property int Id, and I'm not use if it is OK to override id property with new in Sensor class
new Sensor
{
Id = int.Parse($"{sensorValue.StationId}{sensorValue.Id}"),
SensorId = sensorValue.Id,
StationId = sensorValue.StationId,
Name = sensorValue.Name,
MeasuredTime = sensorValue.MeasuredTime,
TimeWindowStart = sensorValue.TimeWindowStart,
TimeWindowEnd = sensorValue.TimeWindowEnd
};
new Sensor
{
Id = int.Parse($"{sensorValue.StationId}{sensorValue.Id}"),
SensorId = sensorValue.Id,
StationId = sensorValue.StationId,
Name = sensorValue.Name,
MeasuredTime = sensorValue.MeasuredTime,
TimeWindowStart = sensorValue.TimeWindowStart,
TimeWindowEnd = sensorValue.TimeWindowEnd
};
Pobiega
Pobiega9mo ago
Not a big fan of that int requirement
Alex
AlexOP9mo ago
I think it's possible to change it to string, but won't there be any "performance" issues if I use string instead of int?
Pobiega
Pobiega9mo ago
consider a GUID then I don't really see why the ID must be a combined id here, other than to guarantee uniqueness
Alex
AlexOP9mo ago
The data that I get has a nested structure 1 station has many sensors (different stations can have the same sensor), and I need to store all the sensor data in way that they don't overwrite each other, but I can update their data. The sensor and station already contains id
Want results from more Discord servers?
Add your server