【问题标题】:How to update Azure Storage Table one specific property values using other property value in where condition?如何在 where 条件下使用其他属性值更新 Azure 存储表一个特定属性值?
【发布时间】:2021-03-01 09:27:06
【问题描述】:

假设我有下面的表存储表 NAMED Tbl_ResourceCost。

现在,我想通过使用分区键和 ResourceGuID 来更新扩展成本。因此,如果我在 WHERE 条件下将 xxx01 作为 PartitionKey 和 zzz01 作为 ResourceGuID 传递,那么它将更新我的扩展成本(即 100 和 200),比如说 700。

如果我们考虑在 SQL 中更新它,那么查询将是“UPDATE Tbl_ResourceCost set ExtendedCost=700 where PartitionKey=xxx01 and ResourceGuID=zzz01”。它将在一次执行中将 100 和 200 更新为 700。

我正在寻找 C# 中的类似查询以在 Azure 存储表中更新。我将 NuGet 包用作:Microsoft.Azure.Cosmos.Table

【问题讨论】:

  • 你有什么更新吗?
  • 谢谢吉姆徐。您之前在评论部分提供的链接帮助我完成了这项任务。

标签: c# azure nosql azure-storage


【解决方案1】:

如果要更新 Azure 表存储实体的属性,我们需要提供分区键和行键。更多详情请参考here。因此,根据您的需要,我们需要查询 Azure 表存储以获取这些您需要的实体,然后更新这些。

例如

using Microsoft.Azure.Cosmos.Table;
using System.Threading.Tasks;
using System;
namespace Storage
{
    class Program
    {
        static async Task Main(string[] args)
        {
           
            string connectionString = "";
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
            CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
            var table =tableClient.GetTableReference("test");

            TableQuery<MyEntity> myQuery = new TableQuery<MyEntity>().Where(
                    TableQuery.CombineFilters(
                    TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, ""),
                        TableOperators.And,
                    TableQuery.GenerateFilterCondition("ResourceGuID", QueryComparisons.Equal,"")));

            foreach (MyEntity entity in table.ExecuteQuery(myQuery))
            {
                entity.ExtendedCost = ;
                TableOperation insertOrMergeOperation = TableOperation.InsertOrMerge(entity);
                TableResult result = await table.ExecuteAsync(insertOrMergeOperation);
                var s = result.Result as MyEntity;
               Console.WriteLine(s.ExtendedCost )
            }

        }      
    }

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

  
        public string ResourceGuID{ get; set; }

        public Int32 ExtendedCost { get; set; }

    }
}

【讨论】:

  • 感谢您的支持。它帮助了我。我一直在寻找完全相同的。
猜你喜欢
  • 2022-01-21
  • 2020-10-10
  • 2017-08-05
  • 1970-01-01
  • 1970-01-01
  • 2021-01-27
  • 2019-04-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多