【问题标题】:Read Table storage array in c#在c#中读取表存储数组
【发布时间】:2020-02-12 09:26:42
【问题描述】:

在表存储表中,我有一个数组字段。

如何在 c# 中阅读它?

我尝试将字段映射为字符串:

public string @params { get; set; }

但我总是将“数组”作为其内容。

按照 Kevin 的建议,我将模型修改为 folloes:

    public string @params { get; set; }

    [IgnoreProperty]
    public ObjParams[] param
    {
        get
        {
            if (@params != null)
                return JsonConvert.DeserializeObject<ObjParams[]>(@params);
            else
                return null;
        }
        set
        {
            @params = JsonConvert.SerializeObject(value);
        }
    }

但反序列化失败,因为@params 总是等于'Array'。

用于填充参数的流分析查询如下:

SELECT
    IoTHub.ConnectionDeviceId as PartitionKey,
    RowKey =  TRY_CAST( TRY_CAST( DATEDIFF( SECOND, DATETIMEFROMPARTS(1970, 1, 1, 0, 0, 0, 0), i.mts) AS NVARCHAR(MAX) ) + '00' as FLOAT) , -- unix epoch timestamp & Line & TimeSlot
    mts = TRY_CAST(i.mts AS DATETIME),
    ver = i.ver,
    eventId = i.eventId,
    tags = i.tags,
    params = i.params,
    CASE WHEN YEAR(TRY_CAST(i.StartTS as DATETIME)) = 1970  THEN NULL ELSE TRY_CAST(i.StartTS AS DATETIME) END as StartTS,
    CASE WHEN YEAR(TRY_CAST(i.EndTS as DATETIME)) = 1970  THEN NULL ELSE TRY_CAST(i.EndTS AS DATETIME) END as EndTS,
    CASE WHEN YEAR(TRY_CAST(i.StartTS as DATETIME)) > 1970  and YEAR(TRY_CAST(i.EndTS as DATETIME)) > 1970 
        THEN DATEDIFF(second, i.StartTS, i.EndTS) ELSE 0 END as DurationTS,
    no = i.no,
    severity = i.severity,
    L = i.L

    --i.*        
FROM
    IoTHubInput00 AS i 
    PARTITION BY PartitionId -- partitionby per performance e scalabilità
    TIMESTAMP BY TRY_CAST(i.mts AS DATETIME)
WHERE  
    i.type ='event' 

在源 i.params 中是一个 JSON。

源中的 JSON 类似于以下示例:

 "params": [
    {
        "tipo": "ssid",
        "value": 0
    },
    {
        "tipo": "rssi",
        "value": 0
    }
 ],

更新

我改变模型如下:

public class EventTS : TableEntity
{
    public EventTS() { }

    public EventTS(string partitionKey, string rowKey)
    {
        this.PartitionKey = partitionKey;
        this.RowKey = rowKey;
    }

    public string uid {
        get
        {
            return this.RowKey;
        } 
    }

    public long DurationTS { get; set; }
    public DateTime EndTS { get; set; }
    public string L { get; set; }
    public long eventId { get; set; }
    public DateTime mts { get; set; }
    public long no { get; set; }

    [EntityPropertyConverter]
    public List<ObjParams> @params { get; set; }

    public override IDictionary<string, EntityProperty> WriteEntity(OperationContext operationContext)
    {
        var results = base.WriteEntity(operationContext);
        EntityPropertyConvert.Serialize(this, results);
        return results;
    }

    public override void ReadEntity(IDictionary<string, EntityProperty> properties, OperationContext operationContext)
    {
        base.ReadEntity(properties, operationContext);
        EntityPropertyConvert.DeSerialize(this, properties);
    }

    public long severity { get; set; }
    public string tags { get; set; }
    public string ver { get; set; }

    public string decodifica_evento { get; set; }
    public EventsTypeDescription descrizione_evento { get; set; }

    public bool BLedAcceso { get; set; }
    public long BLedSeverity { get; set; }
    public string tsUTCOffset { get; set; }
    public string startsUTCOffset { get; set; }
    public string endtsUTCOffset { get; set; }
    public string IoTHubError { get; set; }
}

但是当调用 ReadEntity 时,属性字典不包含参数。为什么?

谢谢。

【问题讨论】:

  • 列中有什么数组?这只是一个数组的json字符串吗?
  • @KevinSmith 只是一个数组的json字符串。

标签: c# azure-table-storage


【解决方案1】:

默认情况下,C# 驱动程序不适用于数组或复杂类型,解决方法是创建一个属性来为您的数组属性来回进行序列化

public class MyEntity : TableEntity
{
    public string MyPropery { get; set; }

    [IgnoreProperty]
    public string[] MyProperyArray
    {
        get
        {
            return JsonConvert.DeserializeObject<string[]>(MyPropery);
        }
        set
        {
            MyPropery = JsonConvert.SerializeObject(value);
        }
    }
}

【讨论】:

  • 凯文请看我的问题整合。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多