如何动态存储1个具有4个值的键

Rot*_*rak 0 c# tuples unity-game-engine

我想[timestamp as int]在启动应用程序时保存默认数量的数据集(1个唯一键和4个不同的传感器值).从那一刻开始,新数据集每隔10秒就会保存在数据库中.这些数据集应添加到存储中,例如元组列表.最后,我正在使用存储的数据绘制图表.

需要存储的不同值如下:

  • 时间戳: int
  • 湿度传感器: float
  • 温度.传感器:float
  • 光传感器: int
  • 按钮"传感器": boolean

不幸的是,Unity Mono似乎不支持具有4个值的元组.因此,此代码无效:

List<Tuple<int, float, float, int, boolean>> list = new List<Tuple<int, float, float, int, boolean>>();
Run Code Online (Sandbox Code Playgroud)

它总是会弹出消息,该类型Tuple需要2个类型参数.Dictionary'pro是关键值(我的时间戳),但另一方面我也只能存储两个值(包括键).

一个List元组是完美的,因为如果用户选择"显示最后10个值",我们添加一个新的数据集,并删除最旧的数据集.

还有另一种方法吗?

Gil*_*een 5

不要像Tuple4-5值那样工作..很难跟踪每个Item意味着什么并导致错误.创建自定义类:

public class SensorData
{
    public int TimeStamp { get; set; }
    public float Humidity { get; set; }
    public int Temp { get; set; }
    public int Light { get; set; }
    public bool Button { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

然后,如果你想要一个列表/字典:

List<SensorData> list = new List<SensorData>();
Dictionary<int, SensorData> mapping = new Dictionary<int, SensorData>();
Run Code Online (Sandbox Code Playgroud)

然后,如果您拥有的数据最初位于列表中,则可以使用该数据.ToDictionary创建字典:

list.ToDictionary(key => key.TimeStamp); 
// Note that this will faild if you have sevetal items with the same timestamp
// If not unique then look at `.GroupBy` or `LookUp`
Run Code Online (Sandbox Code Playgroud)