如何在条件应用程序中使用Azure表中检索到的行的属性?

如何在条件应用程序中使用Azure表中检索到的行的属性?,azure,azure-functions,azure-table-storage,Azure,Azure Functions,Azure Table Storage,我有一个我正在填充/更新的azure表,我只想在行不存在时创建一行,或者在行缺少属性时更新该行。我有下面的行检查函数,但我不知道如何检查该行的属性是否为空 纬度和经度是行属性,城市和州也是 public static async Task<bool> rowExists(CloudTable table, string city, string state) { TableOperation tOP = TableOperation.Retri

我有一个我正在填充/更新的azure表,我只想在行不存在时创建一行,或者在行缺少属性时更新该行。我有下面的行检查函数,但我不知道如何检查该行的属性是否为空

纬度和经度是行属性,城市和州也是

        public static async Task<bool> rowExists(CloudTable table, string city, string state)
    {
        TableOperation tOP = TableOperation.Retrieve(state, city);
        var result = await table.ExecuteAsync(tOP).ConfigureAwait(false);
        if (result.Result == null)
        {
            return false;
        }
/*          else if(row latitude or longitude == null)
        {
            return false;
        } 
*/
        else
            return true;
    }
编辑:


为了让问题更清楚,我的最终目标是在不存在的情况下插入一行。这部分很好用。如果该行存在,但缺少纬度或经度(例如,由于API的查询限制已达到),我想更新该行以获取缺少的属性。

解决此问题的基本方法之一是将结果转换为DynamicTableEntity对象,并检查其中是否存在特定属性。例如,您可以使用如下代码:

    public static async Task<bool> RowExists(CloudTable table, string city, string state)
    {
        TableOperation tOP = TableOperation.Retrieve(state, city);
        var result = await table.ExecuteAsync(tOP).ConfigureAwait(false);
        var entity = result.Result as DynamicTableEntity;
        if (entity == null) return false;//Entity does not exists.
        if (!entity.Properties.ContainsKey("Latitude") || !entity.Properties.ContainsKey("Longitude")) return false;//Either of these attributes do not exist in the entity.
        return true;//All's well. Let's move on to the next record :)
    }

一个后续问题:如果查询表并发现实体中存在纬度和经度,那么您不想做任何事情。这是正确的吗?或者,即使存在这两个属性,您是否可以更新该记录?您是正确的。如果这两个属性都存在,那么这就是我所需要的,我只需要转到列表中的下一个实体。我只是想减少API调用,这样就不会超过我的查询限制。假设API调用指的是对表存储的调用,那么即使是检查,也要对表存储进行调用。您可以简单地使用表存储中可用的Upsert方法,而不是先检查然后插入或更新。如果该行不存在,这些方法将插入该行,否则将更新该行。这个解决方案对你有用吗?哦,对不起,我想我也应该澄清一下。。。我正在使用谷歌地图API来获取纬度和经度,不需要道歉……我现在有点不知所措:。给你贴一个答案…看看是否有帮助。我会试试看。非常感谢。