问题描述
我正在构建项目以存储来自 IoT 设备的传感器数据。我使用 IoT 中心、流分析和表存储。
我有 Web API,我想从表存储中的表返回值。我不知道为什么我只在自定义列中从 Table Storage 响应中获得空值。我知道 Table Storage 中的列是区分大小写的,我记得这一点。
当我从 .NET SDK 添加 Row 到 Table 并且接下来也使用 SDK 查询它时,真正有趣的是什么,我得到了正确的值(不是 null :))
这是 Azure 门户中存储资源管理器中数据的样子:
这表明列具有正确的区分大小写的名称:
这是我的代码中的 TableValue 实体:
public class TableValue : TableEntity
{
public TableValue() { }
public TableValue(string id,string deviceid)
{
PartitionKey = deviceid;
RowKey = id;
}
public string Humidity { get; set; }
public string Pressure { get; set; }
public string Temperature { get; set; }
public string SentTimestamp { get; set; }
}
public async Task<List<TableValue>> Handle(Query request,CancellationToken cancellationToken)
{
var table = _tableClient.GetTableReference("iotinsights");
var condition = TableQuery.GenerateFilterCondition("PartitionKey",QueryComparisons.Equal,"Develop1");
var query = new TabyleQuery<TableValue>().Where(condition);
var result = table.ExecuteQuery(query);
return await Task.Fromresult(result.ToList());
}
这是我从 Postman 请求中得到的结果:
{
"humidity": null,"pressure": null,"temperature": null,"sentTimestamp": null,"partitionKey": "Develop1","rowKey": "0131560c-d1d7-4dc3-93f0-14a4c676baa5","timestamp": "2020-12-28T12:21:55.9198819+01:00","eTag": "W/\"datetime'2020-12-28T11%3A21%3A55.9198819Z'\""
},{
"humidity": null,"rowKey": "022bc3f8-c8d5-4ff5-88b6-57e53c720aa9","timestamp": "2020-12-28T12:27:22.9991905+01:00","eTag": "W/\"datetime'2020-12-28T11%3A27%3A22.9991905Z'\""
},...
我应该怎么做才能从此表中获得正确的值?
解决方法
我找到了解决方案,这比我想象的要容易。自定义表实体属性必须具有正确的数据类型
public class TableValue : TableEntity
{
public TableValue() { }
public TableValue(string id,string deviceId)
{
PartitionKey = deviceId;
RowKey = id;
}
public Double? Humidity { get; set; }
public Double? Pressure { get; set; }
public Double? Temperature { get; set; }
public DateTime? SentTimestamp { get; set; }
}