表上的Azure存储查询返回空列

时间:2016-10-06 12:31:07

标签: azure-table-storage azure-iot-hub

目前我有一个物联网设备模拟器,它按照C#代码向IOT HUB发送消息

   for (int i = 0; i < 50; i++)
    {
        int cnt = new Random().Next(1, 5);
        int Id = new Random().Next(1, 10);

        var telemetryDataPoint = new
        {
            Time = DateTime.Now,
            DeviceId = "myFirstDevice",
            Counter = cnt,
            RestId = Id,

        };
        var messageString = JsonConvert.SerializeObject(telemetryDataPoint);
        var message = new Message(Encoding.ASCII.GetBytes(messageString));

        await deviceClient.SendEventAsync(message);
        Console.WriteLine("{0} > Sending message: {1}", DateTime.Now, messageString);

    }

现在我有另一个C#控制台应用程序,它试图从azure表存储中读取数据

public class SimulatorDeviceEntity : TableEntity
{
    public SimulatorDeviceEntity(string devId, string time )
    {
        this.PartitionKey = devId;
        this.RowKey = time;
    }

    public SimulatorDeviceEntity() { }
    public string counter { get; set; }
    public string restid { get; set; }
    public string deviceid { get; set; }
}


    static void Main(string[] args)
    {
        StorageCredentials creds = new StorageCredentials(accountName, accountKey);
        CloudStorageAccount account = new CloudStorageAccount(creds, useHttps: true);

        CloudTableClient client = account.CreateCloudTableClient();




        TableQuery<SimulatorDeviceEntity> query = new TableQuery<SimulatorDeviceEntity>().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "myFirstDevice"));


        foreach (SimulatorDeviceEntity entity in table.ExecuteQuery(query))
        {
            Console.WriteLine("{0}, {1}\t{2}\t{3}", entity.PartitionKey, entity.RowKey,
                entity.counter, entity.restid);
        }
      }

现在问题我面对的是我得到的是我从表中获取所有记录但是 1)列couter,restId 中的空条目 2)在PartitionKey和RowKey

中更正值

如何获取计数器和restid的正确值

Azure存储表:SimulatorDevice2是截图 enter image description here

1 个答案:

答案 0 :(得分:0)

尝试更改计数器的类型,并将从字符串重新分配到Int64

public class SimulatorDeviceEntity : TableEntity
{
    public SimulatorDeviceEntity(string devId, string time)
    {
        this.PartitionKey = devId;
        this.RowKey = time;
    }

    public SimulatorDeviceEntity() { }

    public Int64 counter { get; set; }
    public Int64 restid { get; set; }
    public string deviceId { get; set; }
}

同样,如果要映射“time”属性,则为DateTime类型。